Java class object

Source: Internet
Author: User

Nice article.

Original Address:(reproduced) Class and class objects in Javaalbert1017

This article is used for note-taking, citing online information:

Http://www.blogjava.net/formatmyself/articles/21291.html is a more comprehensive book.

http://www.open-open.com/lib/view/1328753336405 example is better

Http://zhidao.baidu.com/question/386608758.html explained concisely and clearly.

Java programs run at runtime and the Java Runtime system always identifies all objects as run-time types. This information records the classes each object belongs to. A virtual machine typically uses run-time type information to perform the correct method, and the class that holds the type information is the classes class. The class class encapsulates the state of an object and an interface runtime, and when the class is loaded, objects of class type are automatically created.
Class has no public constructor method. Class objects are constructed automatically by the Java virtual machine when the class is loaded and by invoking the DefineClass method in the ClassLoader, so you cannot explicitly declare a class object.
Virtual machines manage a unique class object for each type. In other words, each class (type) has a class object. When you run the program, the Java Virtual Machine (JVM) first checks to see if the class object for the classes that you want to load is already loaded. If it is not loaded, the JVM will look up the. class file based on the class name and load its class object.
The basic Java types (Boolean, Byte, char, short, int, long, float, and double) and the keyword void also correspond to a Class object.
Each array belongs to a class that is mapped to a class object, and all arrays that have the same element type and number of dimensions share the class object.
A class object in general is loaded into memory, and it is used to create all objects of that class.

The class object is generated in the following way:

1.class.forname ("Class name string")

2. Class name.

3. Instance object. GetClass ()

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

  1. public class TestClass {
  2. public static void Main (string[] args)
  3. {
  4. try {
  5. Test Class.forName ()
  6. Class testtypeforname=class.forname ("Testclasstype");
  7. System.out.println ("Testforname---" +testtypeforname);
  8. Test class name. class
  9. Class Testtypeclass=testclasstype.class;
  10. System.out.println ("Testtypeclass---" +testtypeclass);
  11. Test Object.getclass ()
  12. Testclasstype testgetclass= new Testclasstype ();
  13. System.out.println ("Testgetclass---" +testgetclass.getclass ());
  14. } catch (ClassNotFoundException e) {
  15. TODO auto-generated Catch block
  16. E.printstacktrace ();
  17. }
  18. }
  19. }
  20. Class testclasstype{
  21. constructor function
  22. Public Testclasstype () {
  23. System.out.println ("----constructor---");
  24. }
  25. Static parameter initialization
  26. static{
  27. SYSTEM.OUT.PRINTLN ("---static parameter initialization---");
  28. }
  29. Non-static parameter initialization
  30. {
  31. SYSTEM.OUT.PRINTLN ("----non-static parameter initialization---");
  32. }
  33. }

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

Java class object

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.