Detailed class object in Java Class

Class Class Description:

  In the java world, everything is an object. In a sense, java There are two kinds of objects: Class instance of an object and the object.

  Runtime type information of each class is represented by Class object. It contains information about the class. In fact, we came to the object instance created by the Class object.

  Java Class object performs its use RTTI (runtime type identification, Run-Time Type Identification), polymorphism is implemented based RTTI.

  Each class has a Class object, a new class whenever the compiler generates a Class object, primitive type (boolean, byte, char, short , int, long, float, and double) with a Class object, there is an array of Class objects, even the keyword void are also Class objects (void.class). Class object corresponding java.lang.Class class, if the class is an object of abstract and collections, then Class is the class of the class abstract and collections.

  Class no public constructor class, Class object when the class is loaded by the Java virtual machine and an automatic calling method defineClass configured by the class loader , it is not explicitly declared a Class object. A class is loaded into memory and for us to use to go through the following three stages:

  1. Load, which is performed by the class loader (ClassLoader) a. To obtain a binary byte stream whose definition (Class byte code) by the fully qualified name of a class, the byte stream that represents the storage structure into a static method to runtime data interface, in accordance with the java bytecode Representative java.lang.Class generate a heap objects of this class.

  2. Link. Link stage verification information byte stream Class file contains meets the requirements of the current virtual machine, static domain allocating storage space and set the initial value of the class variable (the default value of zero), and, if necessary, then, the constant reference symbol into the pool direct reference.

  3. initialized. To this stage, really began to execute java program code defined in the class. Static class initializer for performing static and initial block, if the class has a parent class, then the parent class is initialized preferentially.

  All classes are used once in their first, dynamically loaded into the JVM ( lazy loading ). When the program first to create a static member of the class of reference, it will load the class. Class object is created using the new time will be treated as a static member of the class of reference. Java program is therefore a program is not fully loaded before it starts to run, in its various classes are loaded only when necessary. This is different from many traditional languages. The behavior of dynamic load capacity, static load is difficult languages such as C ++ or impossible to replicate.

  In the class loading phase, the class loader first checks whether the Class object of this class have been loaded. If you have not loaded, the default class loader looks for .class files based on the fully qualified name of the class. When the bytecode classes are loaded, they will accept verification, to ensure that it is not damaged, and does not contain bad java code. Once the Class object of a class is loaded into memory, we can use it to create all objects of this class.

Dog.class、dog.getClass、Class.forName区别:

. 1  class Dog {
 2  
. 3      // located in a static constant pool 
. 4      static  Final String name = "Tom" ;
 . 5      static String Sex = "Tom IS man" ;
 . 6  
. 7      // initialization is performed at the time the class is loaded. 
. 8      static {
 . 9          System.out.println ( "Loading Tom" );
 10      }
 . 11  }
 12 is  
13 is  class Cat {
 14  
15      // placed in the static constant pool 
16      static  Final String name = "Mark" ;
 . 17      staticSex = String "Mark IS man" ;
 18 is  
. 19      // initialization is performed at the time the class is loaded. 
20 is      static {
 21 is          System.out.println ( "Loading Mark" );
 22 is      }
 23 is  }
 24  
25  public  class ClassInfo {
 26 is  
27      public  static  void main (String [] args) throws a ClassNotFoundException, an InstantiationException is, {IllegalAccessException
 28          the System.out. the println ( "- Start DOG" );
 29          . class = Dog Dog class ;   //Dog using the JVM class loader class, the class is loaded into memory Dog (provided: Dog class is loaded into memory yet), do not Dog class initialization class return Class object class of Dog. 
30          System.out.println ( "-----------" );
 31 is          System.out.println (Dog.name);
 32          System.out.println ( "-------- --- " );
 33 is          System.out.println (Dog.sex); // decompile stars, initializing Dog object at this time. 
34 is          System.out.println ( "- Start Cat-" );
 35          Class CAT = the Class.forName ( "Cat"); // charged Cat class, the class and do initialization. 
36          System.out.println ( "-----------" );
 37 [          System.out.println (Cat.name);
 38 is          System.out.println ( ");
 39          System.out.println (Cat.sex);
 40  
41 is          Dog daHuang = new new Dog ();
 42 is          Class daHuangClass daHuang.getClass = (); // return a reference to an object refers to the real daHuangClass run (because: son class object reference to the object may be assigned to the parent object reference variable) belongs to the class. 
43 is  
44 is          . Class <Cat> Cat xiaobai = class ;   // get class object 
45          Cat instance xiaoBai.newInstance = (); // returns cat objects
 46          // when using Class objects newInstance () method, we must ensure that: 1, this class has been loaded; 2, this class has been connected.
47          // completed the above steps exactly two Class static methods forName () completed, this static method calls start class loader that loads the loader java API's.
48 
49          // difference between the new keyword and newInstance () method:
 50          // the newInstance: weak type. ineffective. Only call the constructor with no arguments.
51          // new new: a strongly typed. Relatively efficient. You can call any public structure. 
52      }
 53 is  
54 is }

 

Recommended reading: https://blog.csdn.net/forrestgtju/article/details/92594144

     https://blog.csdn.net/Activity_Time/article/details/96865451

     https://blog.csdn.net/hanleijun/article/details/24576539

Guess you like

Origin www.cnblogs.com/mxh-java/p/11918888.html