Anatomy of the Java Reflection Mechanism (III.)-Class type

Source: Internet
Author: User

in order to distinguish it from our usual class, we have given a Chinese name class type. The purpose is to know that this class is non-class. The previous blog has introduced Class Loader, which serves to load classes into memory based on the information provided. I have previously mentioned that the provider of this type of information is the class that this blog will introduce. Reminder: A class object and its instance are not the same, remember, or you will be confused. Here we go!
ConceptClass is an abstraction of the common information for all classes (note the object), such as the interface that the class implements, the corresponding loader, the class name, and so on. In a word, class types hold the type information corresponding to each class. Each class has a class object that is automatically constructed by the JVM after the class is loaded. It is also managed by the JVM, and class classes are not public constructed methods.
Class objects are like, for example, DNA for each person, which has all of your biological information. In Java, you can get instances of classes by class, and another instance of you may be available in the future through your DNA. Sci-fi movies have come true. OK, the concept should have a preliminary understanding.
Common Methods

The introduction of the method should not be so simple, but found that the two sentences are not clear, and for the understanding of Java has a good help. So temporarily decided to write a separate blog for this part. Here is a simple list of some of the methods used before.

  • Forname: Returns the class object associated with the classes or interfaces with the given string name.
  • GetName (): A Class object describes the properties of a particular class, and the most common method in the class class GetName returns the name of the entity (class, interface, array class, base type, or void) represented by this class object as a String.
  • Newinstance (): Creates a new instance of the type described by the class object. The Newinstance () method invokes the default constructor (no parameter constructor) to initialize the new object.
  • getClassLoader (): Returns the class loader for this class.
  • Getinterfaces (): Determines the interface that this object represents for the implementation of the class or interface.
  • Getcomponenttype (): Returns the Class that represents the array component type.
  • Getsuperclass (): Returns a Class object that represents the superclass of the Entity (class, interface, base type, or void) represented by this class
  • IsArray (): Determines whether this class object represents an array class.

How to getThere are three ways to get a class object
(1) using the Object.getclass () method to obtain the class instance of the object;
(2) using the Class.forName () static method, get a class instance by its name
(3) using the class of the classes to get the class instance, for the basic data type of the encapsulation class, you can also use the. Type to get a class instance of the corresponding base data type
It is important to note that the virtual machine generates only one byte code, which can be used to produce multiple instance objects. This means that the class object will only have one. Look at the following code:
Test Class
public class Test {static   {   System.out.println ("statically initialized");   }   {   System.out.println ("Non-static Initialization");}   }
Client
public class Client {public    static void Main (string[] arg) throws ClassNotFoundException, Instantiationexception, Il legalaccessexception{               //Method 1       Class c=class.forname ("Com.zjj.ClassTest.Test");               Method 2    C=test.class;               Method 3    Test t=new test ();    C=t.getclass ();               Test t2=new Test ();}    }
The output is:
Static initialization
Non-static initialization
Non-static initialization
It is known that the static initialization method is executed when the class is loaded, and the non-static initialization method is executed when the class is instantiated. The output only prints a "static initialization", which means that the class object three times is the same.

That is, the Java Virtual Machine (JVM) checks whether a class object of that type is loaded if we are going to produce an object of a certain classes or a class object of that kind, during runtime. If it is not loaded, the JVM will find the. class file and load it based on the name of the class. Once a class object of a type has been loaded into memory, it can be used to produce all objects of that type

Conclusion This summary : At this point, it should be understandable that class is also a class, but it is an abstraction of all classes, and the name is as easy to confuse as the class we know. In general, each class has a corresponding class object that holds the information for this class, which is constructed and managed by the JVM. The existence of a class object is the basis of Java reflection.

Reflection Mechanism Summary : The reflection mechanism is an important part of Java, providing Java with the ability to load classes, that is, the dynamic nature. Class is the information provider, and class loader is the loading tool, both of which are the most fundamental parts of the reflection mechanism. So the so-called reflection is the decoupling, the way is to get the information of unknown class through class, and then instantiate. Of course, the work done by class loader is hidden and is called by the class object. So you don't have to show the call yourself. So, three blog posts are over!

By the way, the content from this blog, plus a blog, the content has not much to do with the reflection mechanism but helps to deepen the understanding of the Java operating mechanism.

Anatomy of the Java Reflection Mechanism (III.)-Class type

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.