A detailed description of class objects in Java

Source: Internet
Author: User

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?
  1. /**
  2. * 2012-2-6
  3. * Administrator
  4. */
  5. /**
  6. * @author: Liang
  7. * File name: Testclass.java
  8. * Time: 2012-2-6-10:01:52
  9. */
  10. Public class TestClass {
  11. Public static void Main (string[] args)
  12. {
  13. try {
  14. Test Class.forName ()
  15. Class testtypeforname=class.forname ("Testclasstype");
  16. System.out.println ("Testforname---" +testtypeforname);
  17. Test class name. class
  18. Class Testtypeclass=testclasstype.  class;
  19. System.out.println ("Testtypeclass---" +testtypeclass);
  20. Test Object.getclass ()
  21. Testclasstype testgetclass= New Testclasstype ();
  22. System.out.println ("Testgetclass---" +testgetclass.getclass ());
  23. } catch (ClassNotFoundException e) {
  24. TODO auto-generated Catch block
  25. E.printstacktrace ();
  26. }
  27. }
  28. }
  29. class testclasstype{
  30. constructor function
  31. Public Testclasstype () {
  32. System.out.println ("----constructor---");
  33. }
  34. Static parameter initialization
  35. static{
  36. System.out.println ("---static parameter initialization---");
  37. }
  38. Non-static parameter initialization
  39. {
  40. System.out.println ("----non-static parameter initialization---");
  41. }
  42. }

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

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.