Java reflection beginners

Source: Internet
Author: User

Class -- java Class abstraction, is a special Class.

At the beginning, we understood that every java class defined by Class, whether defined by the system or custom class, is a Class instance. Now we are self-correcting, class instances are the bytecode of each Class in the memory. According to instructor Zhang, the bytecode of each Class is different. Therefore, the content stored in the memory for each Class is different, the objects corresponding to these different memory spaces are instances of the Class (a pointer to the bytecode of the java Class ).

Obtain the Class Object

Three methods are used to obtain the Class Object:

1. Specify the bytecode object Class cls = x. class directly;

2. Obtained by calling the getClass () method through the Class Object

3. Use the Class. forname (String str) method to specify

 

The Constructor class is an abstraction of java class Constructor. It constructs class instances through reflection,

Constructor constr = new String (). getClass (). getConstructor (x. class...); // x. class is the Class Object of the parameter corresponding to the Constructor, Variable Parameter

String str = constr. newInstance (x...); // x is a constructor parameter. It is not a Class object, but must be an instance object of x. class. It can be a variable parameter.

Use new String (). getClass (). newInstance () to call a construction method without Parameters

 

 

Field is an abstraction of the member variables of the java class. Each Field object is a class attribute.

String heima = new String ("heima ");

Field field = heima. getClass (). getField (x); // x indicates the attribute name and the type is String.

Field. get (heima); // heima is the object of the property of the value to be obtained.

// Field and pointer to class variables are very similar

For obtaining private variables, getField () can only obtain visible variables, but not private variables.

Field field = new Object (). getClass (). getDeclaredField (x); // x indicates the attribute name and the type is String. Obtain the declared attribute of the specified class.

The field object cannot be retrieved.

Field. setAccessible (true); // if this parameter is set to accessible, the attributes of the field are no longer restricted by private (a drag-and-drop method)

Field. get (obj );

 

 

Method is the abstraction of methods in java classes. Each Method object is a Method of a class.

Method method = new String (). getClass (). getMethod (String strName, x. class ...); // obtain the specified method of the class. The strName parameter of getMethod () is the method name, String type, x. class is a method parameter type, a Class object, and a variable parameter.

Method. invoke (obj, x); // call method. obj is the object of the call method. If it is a static method, it can be null. x is x. class instance object, that is, method parameters

Like variables, you can use the same method to call private methods of classes.

Customize a class TestPoint

Public class TestPoint {
 
Public String name;
 
Public TestPoint (String name ){
Super ();
This. name = name;
}

Private void toString ()
{
System. out. println (name );
}
 
}

Call the toString () method through reflection

TestPoint tp = new TestPoint ("this is a private method call ");
Method tpMethod = tp. getClass (). getDeclaredMethod ("toString"); // because there is no parameter, there is only one parameter for the Method name.
TpMethod. setAccessible (true );
TpMethod. invoke (tp); // there is only one parameter here

 

The foundation of reflection is the Class. Reflection operations can be performed only when the Class object is obtained first.

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.