"Go" class object in Java and class name. class, Class.forName (), getclass () difference

Source: Internet
Author: User

The class object is generated in the following way:

1. Class Name: The JVM will use the class loader to load the class into memory (provided that the class has not been loaded into memory) and does not do the initialization of the class. Returns the object of class

2.class.forname ("Class name string") (Note: Class name string is package name + class name) Description: Load class and do static initialization of class, return class object

3. Instance object. GetClass () Description: Static initialization of the class, non-static initialization; Returns the object of the class to which the reference runtime really refers (because: a reference to a child object may be assigned to a reference variable in the parent object)

Use the following procedure to see how the Class object is generated.

[Java]View PlainCopy  
  1. Package classtest;
  2. Public class TestClass {
  3. public static void Main (string[] args) {
  4. try {
  5. //Test. Class
  6. @SuppressWarnings ("Rawtypes")
  7. Class Testtypeclass = Testclasstype.  class;
  8. System.out.println ("Testtypeclass---" + testtypeclass);
  9. //Test class.forname ()
  10. @SuppressWarnings ("Rawtypes")
  11. Class testtypeforname = Class.forName ("Classtest.testclasstype");
  12. System.out.println ("testtypeforname---" + testtypeforname);
  13. //Test Object.getclass ()
  14. Testclasstype Testtypegetclass = new Testclasstype ();
  15. System.out.println ("Testtypegetclass---"
  16. + Testtypegetclass.getclass ());
  17. } catch (ClassNotFoundException e) {
  18. //TODO auto-generated catch block
  19. E.printstacktrace ();
  20. }
  21. }
  22. }

[Java]View PlainCopy 
  1. Package classtest;
  2. Public class Testclasstype {
  3. //Constructors
  4. Public Testclasstype () {
  5. System.out.println ("----constructor---");
  6. }
  7. //static parameter initialization
  8. Static {
  9. System.out.println ("---static parameter initialization---");
  10. }
  11. //non-static parameter initialization
  12. {
  13. System.out.println ("----non-static parameter initialization---");
  14. }
  15. }



The operation results are as follows

[Java]View PlainCopy  
    1. Testtypeclass---class classtest.testclasstype
    2. ---static parameters are initialized---
    3. Testtypeforname---class classtest.testclasstype
    4. ----non-static parameter initialization---
    5. ----constructor---
    6. Testtypegetclass---class classtest.testclasstype

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

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.

from:http://blog.csdn.net/zhangxichao100/article/details/51104971

"Go" class object in Java and class name. class, Class.forName (), getclass () difference

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.