Java Class Object details and class name. class, Class. forName (), getClass () difference,. classgetclass

Source: Internet
Author: User

Java Class Object details and class name. class, Class. forName (), getClass () difference,. classgetclass

The Class object is generated as follows:

 

1. class Name. Class description: JVM will use the class loader to load the Class into the memory (provided that the class has not been loaded into the memory) without Class initialization. class objects will be returned.

 

2. class. forName ("Class Name string") (Note: Class Name string is package name + Class name) Description: load the Class, perform static Class initialization, and return the Class Object

 

3. instance Object. getClass () Description: Performs static initialization and non-static Initialization on the class; returns the object actually referred to during the reference run (because the reference of the sub-object may be assigned to the reference variable of the parent object) class Object of the Class

 

The following program is used to observe the principle of generating a Class object.

 

[Java]View plain copy
  • Package ClassTest;
  • Public class TestClass {
  • Public static void main (String [] args ){
  • Try {
  • // Test. class
  • @ SuppressWarnings ("rawtypes ")
  • Class testTypeClass = TestClassType. class;
  • System. out. println ("testTypeClass ---" + testTypeClass );
  • // Test Class. forName ()
  • @ SuppressWarnings ("rawtypes ")
  • Class testTypeForName = Class. forName ("ClassTest. TestClassType ");
  • System. out. println ("testTypeForName ---" + testTypeForName );
  • // Test Object. getClass ()
  • TestClassType testTypeGetClass = new TestClassType ();
  • System. out. println ("testTypeGetClass ---"
  • + TestTypeGetClass. getClass ());
  • } Catch (ClassNotFoundException e ){
  • // TODO Auto-generated catch block
  • E. printStackTrace ();
  • }
  • }
  • }

  • [Java]View plain copy
  • Package ClassTest;
  • Public class TestClassType {
  • // Constructor
  • Public TestClassType (){
  • System. out. println ("---- constructor ---");
  • }
  • // Static parameter initialization
  • Static {
  • System. out. println ("--- static parameter initialization ---");
  • }
  • // Non-static parameter initialization
  • {
  • System. out. println ("---- non-static parameter initialization ---");
  • }
  • }


  • The running result is as follows:

    [Java]View plain copy
  • TestTypeClass --- class ClassTest. TestClassType
  • --- Static parameter initialization ---
  • TestTypeForName --- class ClassTest. TestClassType
  • ---- Non-static parameter initialization ---
  • ---- Constructor ---
  • TestTypeGetClass --- class ClassTest. TestClassType
  •  

    Based on the results, we can find that the three generated Class objects are the same. In addition, the program only prints "static parameter initialization" once ".

     

    We know that static method property Initialization is initiated when classes are loaded. Instead of static method property initialization, it is loaded when the new class instance object.

     

    Therefore, this program shows that only one Class object is generated in three methods. When generating the Class object, first determine whether the memory has been loaded.

     

    Therefore, the process of generating Class objects is actually as follows:

     

    When we compile a new java class, JVM will compile it into a class object and store it in a. class file with the same name. During runtime, when an object of this class needs to be generated, JVM will check whether the class has been loaded into memory. If it is not loaded, load the. class file into the memory. If it is loaded, the instance object is generated based on the class file.

    Related Article

    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.