Java memory comprehension & Reflection access to private attributes or methods, java memory
After understanding the memory, you can understand everything and various languages. All languages are like this: the memory allocated by local variables is always in the stack, the memory allocated by new things is always in the heap, and the memory allocated by static things is always in the Data zone. The remaining code must be in the code area. All languages are like this.
For a class in the API documentation, if a class can be directly used without introducing a package, the class must be in the java. lang package.
An interface is a set of abstract methods (public) and constant values (public static final.
Abstract classes must have abstract keywords.
Access to private attributes or methods through Java reflection
Reference Source
The AccessibleObject class is the base class of the Field, Method, and Constructor object. It provides the ability to mark reflected objects as canceling the default Java language access control check during use. For public members, default (Package) Access members, protected members, and private members, use the Field, Method, and Constructor objects to set or obtain fields and call methods respectively, or when you create and initialize a new instance of the class, the access check is executed.
When the accessible flag of the reflected object is set to true, it indicates that the reflected object should cancel the Java language access check during use. Otherwise, check. Because the JDK security check takes a lot of time, you can use setAccessible (true) to disable the security check to improve the reflection speed.
Import java. lang. reflect. field; import java. lang. reflect. method;/*** use the Java reflection mechanism to call the private Method * @ author javasingdog **/public class Reflect {public static void main (String [] args) throws Exception {// directly create the object Person person = new Person (); Class <?> PersonType = person. getClass (); // access the private Method // getDeclaredMethod can get all methods, while getMethod can only get public method = personType. getDeclaredMethod ("say", String. class); // method used to suppress Java access modifier check. setAccessible (true); // call method; person is the method of the object. invoke (person, "Hello World! "); // Access the private attribute Field field = personType. getDeclaredField ("name"); field. setAccessible (true); // set the property value; person is the field of the object. set (person, "mongoingdog"); System. out. println ("The Value Of The Field is:" + person. getName () ;}// JavaBean class Person {private String name; // each JavaBean should implement the public Person () {} public String getName () construction method without Parameters () {return name;} private void say (String message) {System. out. println ("You want to say:" + message );}}