In the previous blog we mentioned ClassLoader, knowing that ClassLoader was used to dynamically load a class file into memory, but how did this class file be generated? Where did it come from? This in turn involves another conceptual-java.lang.class.
Class is a special class for Java, which is an abstract class for the classes and interfaces we define. The class class is used to encapsulate information about classes, including classes and interfaces, that are loaded into the JVM. When a class and an interface are loaded into the JVM, an instance of the class class is automatically created to represent the classes, which is what we call the Class object, which collects basic information about the current object, including properties, methods, constructors, interfaces, parent classes, and so on.
Note: The class class does not have a public construction method, and the class object is automatically constructed by the JVM and DefineClass () in the ClassLoader when the classes are loaded. So the class class is the source of information for the ClassLoader.
How do I get a class object?
1. Use the class name. The class method directly obtains the class object associated with the specified, and does not require the existence of an object of that class.
Example:
public class Reflactiontest {@Testpublic void TestClass () throws ClassNotFoundException {class clazz = null;//1. Directly through the class name. CL The way to get clazz = Person.class; System.out.println (Clazz); }
2. Use the Object.getclass () method to get the class object to which the method object belongs.
Example:
public class Reflactiontest {@Test public void TestClass () throws ClassNotFoundException {string str1 = new String ();
string str2 = new String (); Class cl1= Str1.getclass (); Class Cl2 = Str2.getclass (); System.out.println (Cl1.getname ());//Get the full name of the class System.out.println (Cl1==cl2);}
Console output:
Java.lang.String
True
In the above code, the GetClass method is used to return a class object that encapsulates a string type, GetName returns the name of the encapsulated class. Both CL1 and Cl2 return the same object, so Cl1==cl2 returns True.
Note: For the same class, the JVM will only be loaded once, and the class object corresponding to it will only exist, no matter how many instances you instantiate.
3. Using the Class.forName () static method, the method obtains the object associated with the class based on the specified class name. If the class has not been loaded, the method loads the class into the JVM.
Example:
public class Person {public person () {System.out.println (' person () ');} static {System.out.println ("person is initialized");}}
Run the following code:
public class Reflactiontest {@Testpublic void Testnewinstance () throws ClassNotFoundException {String className = "COM.TG B.reflectiontest.person "; Class clazz = Class.forName (className);}
Console output:
person is initialized
The Class.forName () method first loads the class specified by the string into the JVM and returns the class object associated with it, initializes it after the JVM is loaded, and calls the code in the static block. The parameters of the Forname method must be the full specified name of the class.
Other common methods:
- getClassLoader (): The method is used to return the class loader for the class represented by the class object. If the loader for the class is bootstrap, NULL is returned.
- Getsuperclass (): This method returns the Classs object corresponding to the parent class of the Java class represented by this class object.
- IsArray (): Determines whether this class object represents an array class.
- Getinterfaces (): Determines the interface that this object represents for the implementation of the class or interface.
- The Newinstance () method generates a new instance of the class using the selected class object. It calls the parameterless class constructor to generate a new object. For newinstance (), you can use it to create a new object without any objects present.
Example:
<pre name= "code" class= "Java" >public class Reflactiontest {@Testpublic void Testnewinstance () throws Classnotfoundexception,instantiationexception, illegalaccessexception {String className = " Com.tgb.reflectiontest.Person "; Class clazz = Class.forName (className); Object obj = Clazz.newinstance (); System.out.println (obj);}}
The above code first uses Class.forName to get the class object, that is, the method requires the JVM to find and load the specified person class, and execute the static code snippet of person, dynamically load and create the person object. An object is instantiated only when the Newinstance () method is executed.
Note: Classes created with newinstance () must have a default constructor.
Summary: Class is generally a class that collects basic information about the current object (properties, methods, constructors, parent classes, interfaces). Class objects can only be created by the system, and each class can have only one instance. For each class, there is a class-type object that holds the information for this class.
Both Class and ClassLoader are the basis of reflection technology, and we need to master its principles.
Java Reflection Basics (ii)-class class