Class Class
When a Java program runs, there is an identifier that records information about the Class to which all objects belong. The Class used to store this information is the Class Class. The virtual machine manages a unique Class object for each type. That is, each Class (type) has a Class object. When running a program, the Java Virtual Machine (JVM) first checks to see if the Class object corresponding to the Class to be loaded has already been loaded. If it is not loaded, the JVM looks for the.class file based on the class name and loads its class object. Once a Class object of a type has been loaded into memory, it can be used to generate all objects of that type. The virtual machine generates only one copy of the bytecode, which can be used to generate multiple instance objects. Java source programs (.java files) are converted to Java bytecode (.class files) after being compiled by the Java compiler. The classloader is responsible for reading the Java bytecode and converting it into an instance of the Java.lang. Class Class (Class object). That is, in Java, each Java Class has a corresponding Class object that represents the type information of that Java Class.
Class loading concept
When a class is used (such as new an instance of a class), it checks to see if the class has been loaded into memory, and if not, the load operation is performed. Read the corresponding class file data, parse this data, construct an instance of the corresponding class class. The JVM can then use the class, either to instantiate it or to call its static methods. Java also provides interfaces for manually loading classes, such as the class.forname () method. (Essentially, the Class that generates this Class)
The concept of a class loader
As the name implies, a class loader is used to load Java classes into a Java virtual machine. In general, Java virtual machines use Java classes as follows: Java source programs (.java files) are converted to Java bytecode (.class files) after being compiled by the Java compiler. The classloader is responsible for reading the Java bytecode and converting it into an instance of the java.lang.Class Class. Each such instance is used to represent a Java class. An object of this class is created using the newInstance() method of this instance. Basically all classloaders are an instance of the java.lang.ClassLoader class.
Class initialization concept
After the Class is loaded, the JVM has a Class instance that describes the structure of the Class. However, this class can be used only after class initialization, which is the execution of the class’s static statements, including static variable declarations and static code blocks.
Gets the Class object method
Note: Class Class constructors are private and only automatically constructed by the Java virtual machine by calling the defineClass method in the Class loader, so we cannot explicitly declare a Class object in our code.
1. Use static methods of the Class
Class.forName("java.lang.String");
Copy the code
2. Use the.class syntax of the class
Class c = Employee.class;
Copy the code
3. Use the object’s getClass() method
All Java classes inherit from the Object class, which has a method called getClass (). This method is used to get a reference to an object of Class that has been instantiated. This reference refers to an object of Class
Employee e = new Employee();
Class c3 = e.getClass();
Copy the code
Class Class method
Class.forName()
Load the class specified by the parameter and initialize it. Example: Application in JDBC connection database. Why is the class.forname (‘ driver Class name ‘) method executed when connecting to a database using JDBC? The class.forname () method means loading the class file of the driver class into memory, forming an instance of the class class that describes the structure of the driver class, and initializing the driver class so that the JVM can use it.
Add: if you have experience in database development, you will find that there is no newInstance() method when we load the database driver package. Class. ForName (xxx.xx.xx); Some are: class.forname (xxx.xx.xx).newinstance ().
Class. ForName (“”); If there is a static initializer in a class, the JVM will necessarily execute a static snippet of that class. The JDBC specification explicitly requires that the Driver class register itself with DriverManager. That is, the code for any JDBC Driver class must look something like this:
public class MyJDBCDriver implements Driver {
static {
DriverManager.registerDriver(newMyJDBCDriver()); }}Copy the code
Since we are registered in the static initializer, we only need class.forname (xxx.xxx) when using JDBC.