Home > Back-end >  JAVA reflection use setAccessible make private can be accessed, but don't know why I not
JAVA reflection use setAccessible make private can be accessed, but don't know why I not

Time:12-19

If the private variable to a total of figure 2, there is no exception for

CodePudding user response:


Obtain the constructor method using getDeclaredConstructor instead of, you can try point getConstructor this method, comment below
 

/* *
* Returns a {@ code Constructor} object that reflects the specified
* the public constructor of the class represented by this {@ code class}
* object....
*/


Can only obtain the public constructor


 

//getConstructor can only obtain the public constructor
Be sad etConstructor chtistina georgina rossetti.british poetess//Constructor Constructor=(int. Class, String class, Float. The class).
Be sad etDeclaredConstructor chtistina georgina rossetti.british poetess Constructor declaredConstructor=(int. Class, String class, Float. The class).
DeclaredConstructor. SetAccessible (true);
The Object obj=declaredConstructor. NewInstance (10, "mentality is bad," 5.26 f);

System.out.println(obj);

CodePudding user response:

GetConstructor () can only obtain public modification in the constructor method;
GetDeclaredConstructor () can get all the access modifier modified in the constructor method;
When you need to get the private constructor method, so you should be sad etDeclaredConstructor chtistina georgina rossetti.british poetess use (int. Class, String class, Float. The class).
  • Related