A detailed description of class objects in Java

Source: Internet
Author: User

Original source: http://blog.csdn.net/yuebinghaoyuan/article/details/7244123

The generation of class objects and instance objects is mixed in Java, not to mention that there are many ways to generate class objects and generate instance. So only by figuring out the principles, can we understand them deeply. The first thing to do is to generate a class object and then generate instance. What is the way that class objects are generated, and how are they generated secretly?

The class object is generated in the following way:

1.class.forname ("Class name string") (Note: Class name string must be full name, package name + class name)

2. Class name.

3. Instance object. GetClass ()

Through a small program, you can see how the class object is generated.

1  PackageCom.gxf.rtti;2 3  Public classTestClass {4      Public Static voidMain (string[] args) {5         Try {6Class testforname = Class.forName ("Com.gxf.rtti.TestClassType");7System.out.println ("Testforname---" +testforname);8Class Testtypeclass = Testclasstype.class;9System.out.println ("Testtypeclass---" +testtypeclass);TenTestclasstype Testclasstype =NewTestclasstype (); OneClass Testgetclass =Testclasstype.getclass (); ASystem.out.println ("Testgetclass---" +testgetclass); -              -System.out.println (Testforname = = Testtypeclass && Testforname = =testgetclass); the}Catch(ClassNotFoundException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); -         } +  -     } + } A classtestclasstype{ at     Static{ -System.out.println ("--------Static parameter initialization--------"); -     } -      PublicTestclasstype () { -System.out.println ("--------constructor---------"); -     } in     { -SYSTEM.OUT.PRINTLN ("---------non-static parameter initialization-----------"); to     } +}

Output

--------initialization of static parameters--------
Testforname---class Com.gxf.rtti.TestClassType
Testtypeclass---class Com.gxf.rtti.TestClassType
---------initialization of non-static parameters-----------
--------Constructor---------
Testgetclass---class Com.gxf.rtti.TestClassType
True

According to the results, there are three types of class objects that are generated. and three generated class objects only print once "static parameter initialization".

We know that static method property initialization is initialized when the class is loaded. Instead of static method property initialization, it is loaded at the time of the new class instance object.

Therefore, this procedure shows that three ways to generate a class object, in fact, there is only one class object. When generating a class object, first determine if the memory is loaded.

So, the process of generating a class object is actually the same:

When we write a new Java class, the JVM will compile it into a class object, stored in a. class file of the same name. At run time, when the object of this class needs to be generated, the JVM checks to see if the class is already loaded in memory. If it is not loaded, the. class file is loaded into memory. If loaded, the instance object is generated according to the class file.

A detailed description of class objects in Java

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.