Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

反射机制 --> Class.newInstance() && Constructor.newInstance() #1606

Open
airconditionerr opened this issue Mar 9, 2022 · 2 comments
Open
Labels
enhancement New feature or request or suggestion

Comments

@airconditionerr
Copy link

  1. JDK9文档中,Class.newInstance()方法被标识为Deprecated,同时也提到The call clazz.newInstance() can be replaced by clazz.getDeclaredConstructor().newInstance()
  2. The Java™ Tutorials中提到Constructor.newInstance()是首选,原因为:
  • Class.newInstance() can only invoke the zero-argument constructor, while Constructor.newInstance() may invoke any constructor, regardless of the number of parameters.(Class.newInstance()方法只能调用无参数的构造方法,而Constructor.newInstance()方法可以调用任意参数数量的构造方法。)
  • Class.newInstance() throws any exception thrown by the constructor, regardless of whether it is checked or unchecked. Constructor.newInstance() always wraps the thrown exception with an InvocationTargetException.(Class.newInstance()会抛出任何构造器抛出的受检查异常和不受检查异常;Constructor.newInstance()会将抛出的异常包装在InvocationTargetException中抛出。)
  • Class.newInstance() requires that the constructor be visible; Constructor.newInstance() may invoke private constructors under certain circumstances.(Class.newInstance()要求构造方法可见;而Constructor.newInstance()在某些情况下,可以调用私有的构造方法)

Source1:https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
Source2:https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html

@Snailclimb
Copy link
Owner

  1. JDK9文档中,Class.newInstance()方法被标识为Deprecated,同时也提到The call clazz.newInstance() can be replaced by clazz.getDeclaredConstructor().newInstance()
  2. The Java™ Tutorials中提到Constructor.newInstance()是首选,原因为:
  • Class.newInstance() can only invoke the zero-argument constructor, while Constructor.newInstance() may invoke any constructor, regardless of the number of parameters.(Class.newInstance()方法只能调用无参数的构造方法,而Constructor.newInstance()方法可以调用任意参数数量的构造方法。)
  • Class.newInstance() throws any exception thrown by the constructor, regardless of whether it is checked or unchecked. Constructor.newInstance() always wraps the thrown exception with an InvocationTargetException.(Class.newInstance()会抛出任何构造器抛出的受检查异常和不受检查异常;Constructor.newInstance()会将抛出的异常包装在InvocationTargetException中抛出。)
  • Class.newInstance() requires that the constructor be visible; Constructor.newInstance() may invoke private constructors under certain circumstances.(Class.newInstance()要求构造方法可见;而Constructor.newInstance()在某些情况下,可以调用私有的构造方法)

Source1:https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html Source2:https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html

感谢分享👍

@Snailclimb Snailclimb added the enhancement New feature or request or suggestion label Mar 12, 2022
@Przemek270189wav
Copy link

  1. JDK9文档中,Class.newInstance()方法被标识为Deprecated,同时也提到The call clazz.newInstance() can be replaced by clazz.getDeclaredConstructor().newInstance()
  2. The Java™ Tutorials中提到Constructor.newInstance()是首选,原因为:
  • Class.newInstance() can only invoke the zero-argument constructor, while Constructor.newInstance() may invoke any constructor, regardless of the number of parameters.(Class.newInstance()方法只能调用无参数的构造方法,而Constructor.newInstance()方法可以调用任意参数数量的构造方法。)
  • Class.newInstance() throws any exception thrown by the constructor, regardless of whether it is checked or unchecked. Constructor.newInstance() always wraps the thrown exception with an InvocationTargetException.(Class.newInstance()会抛出任何构造器抛出的受检查异常和不受检查异常;Constructor.newInstance()会将抛出的异常包装在InvocationTargetException中抛出。)
  • Class.newInstance() requires that the constructor be visible; Constructor.newInstance() may invoke private constructors under certain circumstances.(Class.newInstance()要求构造方法可见;而Constructor.newInstance()在某些情况下,可以调用私有的构造方法)

Source1:https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
Source2:https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request or suggestion
Projects
None yet
Development

No branches or pull requests

3 participants