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.
[Java]View Plaincopyprint?
- /**
- * 2012-2-6
- * Administrator
- */
- /**
- * @author: Liang
- * File name: Testclass.java
- * Time: 2012-2-6-10:01:52
- */
- Public class TestClass {
- Public static void Main (string[] args)
- {
- try {
- Test Class.forName ()
- Class testtypeforname=class.forname ("Testclasstype");
- System.out.println ("Testforname---" +testtypeforname);
- Test class name. class
- Class Testtypeclass=testclasstype. class;
- System.out.println ("Testtypeclass---" +testtypeclass);
- Test Object.getclass ()
- Testclasstype testgetclass= New Testclasstype ();
- System.out.println ("Testgetclass---" +testgetclass.getclass ());
- } catch (ClassNotFoundException e) {
- TODO auto-generated Catch block
- E.printstacktrace ();
- }
- }
- }
- class testclasstype{
- constructor function
- 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 results of the test are as follows:
---static parameters are initialized---
Testforname---class Testclasstype
Testtypeclass---class Testclasstype
----non-static parameter initialization---
----constructor---
Testgetclass---class Testclasstype
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.
Original: http://blog.csdn.net/yuebinghaoyuan/article/details/7244123