The difference between reflection mechanism and Class.forName, instance object, Class (attribute), instance object GetClass () in Java (GO)

Source: Internet
Author: User

First, the reflection mechanism of Java
Each Java program must be compiled, loaded, connected, and initialized prior to execution, with the following three phases such as:
which



I, load refers to the compiled Java class file (that is, the. class file) in the binary data read into memory, and put it in the run-time data area in the method area, and then the heap to create a Java.lang.Class object, to encapsulate the class in the method area of the data structure.The result is a class object after loading, and more notably: theThe Java.lang.Class object isSingle-Instance, no matter how many objects this class creates, his class object is the only one!!!   and   There are three ways to load and get the class object:        class.forname (full path of the class), instance object. Class (property), instance Object GetClass () . The difference about them will be described below!!!
   ## #另外   (see previous blog: class.forname () load JDBC driver)

    III, Java use of classes are divided into two ways: active use and passive use. Active use such as:

< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >< Span class= "Apple-converted-space" >
   initialization time is the Java program's The first active use , in addition to the above 6, the other use of the class is passive use, will not cause the initialization of the class. And you should be aware of the following:





can be seen here;the duality of the interface: the interface can be treated as a class (because he can be initialized when there is a static variable in the interface), and the interface is an interface that is independent of the class (there is no construction method in the interface, so it cannot be initialized)





Second, Class.forName, instance object. Class (attribute), instance object GetClass () difference

1, the same point:
In these ways, the Java.lang.Class object is obtained (this is the final product that the class mentioned above is loaded with)
For example:
Package demo;
public class A
{
public static void Main (string[] args) throws Exception
{
System.out.println (A.class);//Get Class object by class. class

A = new A ();
System.out.println (A.getclass ());//Get Class object by instance name. GetClass ()
System.out.println (Class.forName ("demo. A "));//Get Class object by Class.forName (full path)
System.out.println ("........ ................)");
System.out.println (a);//create objects in different ways
System.out.println (A.class.newinstance ());
System.out.println (A.getclass (). newinstance ());
System.out.println (Class.forName ("demo. A "). newinstance ());
}
}

Result: Class demo. A
class Demo. A
class Demo. A (here you can also get a class object that is unique)
.......................................
[email protected]
[email protected]
[email protected]
[email protected]

2. Difference:

1) Class Cl=a.class; Jvmwill use the classAthe class loader,the classALoading Memory(the premise is:classAnot yet loaded into memory),No classAinitialization of a class.Return classAof theClassthe Object

2) Class cl= return reference o because : class Span style= "font-family: the song Body;" The object of  

3) Class.forName (" class name "); JAVA people know . Loading class A, and do the initialization of the class

Report:

FromJVMthe point of view, we use the keywordNewwhen creating a class, this class can not be loaded. But usingthe newinstance () of the class objectmethod, you must ensure that:1, this class has been loaded;2, this class is already connected. And the complete two steps above are exactlyClassthe static methodforname ()completed, this static method invokes the Startup class loader, which loads theJava APIthe loader. Now you can see that the class object'snewinstance ()(this usage is similar to the factory pattern in Java) in factNewThis method is decomposed into two steps, that is, the first callClass The Load method loads a class and then instantiates it. the benefits of this step are obvious. We can call theclassthe static Load methodfornameflexibility, providing a means of decoupling. Finally, use the simplest description to distinguishNewkeywords andnewinstance ()the difference between the methods: newinstance:  weak type. Low efficiency. Only parameterless constructs can be called.   new:  strong type. relatively efficient. Can call any public construction.


The reflection mechanism in Java and Class.forName, instance object. Class (attribute), instance Object GetClass () (GO)

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.