Java Memory Understanding && reflection accessing private properties or methods

Source: Internet
Author: User

Understand the memory, understand everything, understand a variety of languages. All the languages are like this: The local variable allocation memory is always inside the stack, new out of the allocated memory is always in the heap, static things allocated memory is always in the data area. The rest of the code must be in the code area. This is true of all languages.
For a class in the API document, if a class can be used without introducing a package, the class must be in the Java.lang package.
An interface is a collection of abstract methods (public) and constant values (public static final).
Abstract classes must have an abstract keyword.

Access private properties or methods of Java reflection

Reference sources

The AccessibleObject class is the base class for field, Method, and constructor objects. It provides the ability to mark reflected objects as being used to cancel the default Java language access control check. For public members, default (packaged) Access members, protected members, and private members, access checks are performed when you use field, method, and constructor objects to set or get fields, invoke methods, or create and initialize new instances of a class, respectively.

When the accessible flag of a reflected object is set to True, the reflected object should cancel the Java language Access check when it is used. The reverse is checked. Because JDK security checks take a lot of time, the security checks are turned off by setaccessible (true) to increase the reflection speed.

ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;/** * Use the Java reflection mechanism to invoke the private method * @author walkingdog * */   Public  class Reflect {       Public Static void Main(string[] args)throwsException {//Create objects directlyPerson person =NewPerson (); Class<?> PersonType = Person.getclass ();//Access private methods        //getdeclaredmethod can get all the methods, and GetMethod can only get publicmethod = Persontype.getdeclaredmethod ("Say", String.class);//Suppress Java check of access modifiersMethod.setaccessible (true);//Call method; person is the objectMethod.invoke (Person,"Hello world!");//Access private propertiesField field = Persontype.getdeclaredfield ("Name"); Field.setaccessible (true);//Set the value for the property;Field.set (Person,"Walkingdog"); System.out.println ("The Value of the Field is:"+ Person.getname ()); }  }//javabeanClass person{PrivateString name;//Each JavaBean should implement the method of non-parametric construction     Public  Person() {} PublicStringGetName() {returnName }Private void say(String message) {System.out.println ("You want to say:"+ message); }  }

Java Memory Understanding && reflection accessing private properties or methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.