Loading of a class in Java
1 when using a class, the JVM uses a classloader to load the class into memory and get the class object of that category;
2 ClassLoader load a class, will check whether the superior ClassLoader has not loaded the class, if it has been loaded, no longer loaded;
Two program blocks
Static program blocks are executed when the class is loaded, and a static block is partially executed when a loads;
If you remove static and become a non-static program block, the non-static program block will not be executed when the Class A is loaded, and it will be executed only when the instance is created, and an instance is executed once;
Package Com.ob.temp;public class User {public static void main (string[] args) throws ClassNotFoundException {Class.fornam E ("Com.ob.temp.a");}} Class A{static{system.out.println ("A Loaded");} String Name;int age; A () {}a (String Name,int age) {this.name=name;this.age=age;} void Say (String name,int age) {System.out.println (name+ ": +age");} void M1 () {System.out.println ("in M1");}}
Two-reflection (Java.lang.reflect)
1 Reflection: Reflection is the acquisition of all information about a running Java class, including construction methods, member variables, methods, etc., obtained by means of the method of the class object corresponding to the classes;
2 Creating Class class objects three ways:
Class.forName ("The complete classpath of the class to be loaded");
Class name. Classes;
The reference to the object. GetClass ();
Class c1=class.forname ("Com.ob.temp.a"); Class C2=a.class; A a=new a (); A a1=new a (); Class C3=a.getclass (); System.out.println (C1==C2); System.out.println (C1==C3);
will return two true;
3 Creating an instance from a class object
Class c1=class.forname ("Com.ob.temp.a"); A a= (a) c1.newinstance (); a.m1 ();
This is returned by the newinstance () method of the class object;
4 Access Construction Method:
Class c1=class.forname ("Com.ob.temp.a"); Constructor[] Cs=c1.getdeclaredconstructors (); for (Constructor C:cs) {System.out.println (C.getname ());} Constructor C=c1.getdeclaredconstructor (String.class,int.class); A a= (a) c.newinstance ("Zhangsan", 5); System.out.println (a.name+ ":" +a.age);}
You can access all of the constructor methods, or you can access the constructor of the specified parameter, and the constructor method object calls the Newinstance method to return the instance, which can contain parameters;
5 Accessing member variables:
Class c1=class.forname ("Com.ob.temp.a"); Field[] Fs=c1.getdeclaredfields (); for (Field F:fs) {System.out.println (F.getname ());} Field Fname=c1.getdeclaredfield ("name"); System.out.println (Fname.getname ());
Like access to a constructor, you can return an array of variables, or you can return a specified variable;
Class c1=class.forname ("Com.ob.temp.a"); A a= (a) c1.newinstance (); Field[] Fs=c1.getdeclaredfields (); for (Field F:fs) {System.out.println (F.getname () + "Initial value is:" +f.get (a)); if (F.gettype (). Equals (String.class)) {F.set (A, "Zhangsan");} if (F.gettype (). Equals (Int.class)) {F.setint (A, 20);} System.out.println (F.getname () + "modified value is:" +f.get (a));}
The class object of the member variable GetType () returns the class object of the type, the class object get (Object obj) of the member variable returns the value of the variable corresponding to the object of obj, modifies the value of the member variable, with setxxx (Object obj, Value) xxx depends on the variable type;
6 Access Methods:
Class c1=class.forname ("Com.ob.temp.a"); A a= (a) c1.newinstance (); Method[] Methods=c1.getdeclaredmethods (); for (Method m:methods) {System.out.println (M.getname ());} Method Me=c1.getdeclaredmethod ("Say", string.class,int.class); System.out.println (Me.getname ());
You can either get an array of methods, or you can access the methods that specify names and parameters;
Class c1=class.forname ("Com.ob.temp.a"); A a= (a) c1.newinstance (); Method Me=c1.getdeclaredmethod ("Say", String.class,int.class); Me.invoke (A, "Zhang", 20);
The class class object called Invoke ("instance object", the parameter list does not have parameters can not be written);
The reflection of Java object-oriented programming