Java Reflection mechanism

Source: Internet
Author: User

Class

1) In an object-oriented world, everything is the object. In the Java language, static members, ordinary data types are not objects, and others are objects. The common data types are made up of packaging classes. We write each class, and the class is instantiated to get an object. So, is the class not an object? Whose object is the class? What class is the class object? We say, everything is object, we write every class, we provide each class, is also an object, class is an instance object of the Java.lang.Class class. For example, write a Class A, which is an instance object of class. I write a student class, which is also an instance object of class. This is said to be called "there is a class named class". Now there is a class, and the name is called a class. Now there is a class, whose name is class. So what is the example of it? Each of our objects belongs to the class, which is its instance.

2) How exactly does this object represent? Now we're going to write a case that means an instance of class,

Class foo{}

This Foo class is an object. Everything is the object of everything. Who's the object? The instance object of the Java.lang.Class.

So, how is the instance object of Foo represented?

Foo foo1 = new Foo ();

So, we know that the class Foo is also an object, the Class class instance object, how to express it?

We can see the structure of class:

/*constructor.  Only the Java Virtual machine creates Class objects. */

Private Class () {}

is a private constructor that cannot be accessed outside of the class. We have no way to create this object directly.

Any class is an instance of class, and this instance object is represented in three ways.

The first way: it's actually telling us that any class has an implied static member variable class

Class C1 = Foo.class;

The second expression: the object of the class has been known to pass the GetClass method

Class C2 = Foo1.getclass ();

Note that this is easy to confuse. Foo1 represents an instance object of Foo, C1,C2 represents an instance object of class, but the object itself shows that the class Foo is an instance object. The description on the official website for C1,C2 represents the class type of the Foo class (class type). The class type of the Foo class refers to the class Foo, which is itself an object, which class object? The class object. We say that the class type refers to itself as an instance of class. But the Foo class itself also has an instance object, which is the instance object of the Foo class. This is separated from the class type of the Foo class. The class type refers to the object of everything, and the class is also an object. This object, we call it the class type.

C1 = = C2//true

Whether C1 or C2 represents the class type of the Foo class, a class can only be an instance object of the class.

If you know the class name, you can use the. class, if you know the object, you can use GetClass ()

The third way of expression:

Class C3 = null;

try{

C3 = Class.forName ("Com.reflext.Foo"); This is the full name of the class, including the package

} catch (ClassNotFoundException e) {

E.printstacktrace ();

}

C2 = = C3//True

The above concept of class is clear.

We can create object instances of the class entirely from the type of the class. That is, an instance of Foo is created by C1 or C2 or C3

This c1 if it is a class class type, then creates an instance object of Class A, if it is Class B type, then the Class B instance object is created. Now that it is the class type of the Foo class, the instance object of the Foo class is created.

So this place needs to do coercion type conversions.

Foo foo = (foo) c1.newinstance (); Need to have a parameterless construction method

Dynamic Load Class

Class.forName ("Full name of the class")

Not only represents the type of the class, it also represents the dynamic load class

Be sure to distinguish between compiling, running

Compile time load class is static load class, runtime load class is dynamic load class

Class Office {public    sataic void Main (string[] args) {
if ("Word". Equals (Arg[0])) {
Word w = new word ();
W.strat ();
}
if ("Excel". Equals (Arg[0])) {
Excel e = new Excel ();
E.strat ();
}
}}

If you compile this program, you will find an error indicating that two classes and two methods cannot be found. If you write another word class and compile, you will still be prompted for 2 errors, and the Excel class and the Run method cannot be found. This is because the new creation object is a static load class, and all of the possible classes need to be loaded at compile time. Even if Excel is not available, Office programs cannot be used even if Word already exists. So in practice, of course we want Word to be there, word can use it, and now word exists, but because Excel doesn't exist, Word can't use it. If there are 100 features in the future, as long as there is a problem with a function, then it will cause another 99 functions can not be used, obviously, this situation is not what we want to see. The function we want to see is, which one you use, and which to load. No need not to load. So we want to load it at run time. The problem can be resolved by dynamically loading the class.

Java Reflection mechanism

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.