The function and usage of Java reflection __java

Source: Internet
Author: User

So what is the reflection of Java?

As you all know, for Java programs to work, you have to let Java classes be loaded by Java virtual machines. Java classes cannot function correctly if they are not loaded by a Java virtual machine. All of the programs we run now know that the class you need is already loaded at compile time.

The reflection mechanism of Java is that the compiler is not sure which class is loaded, but only loading, detecting and self examining when the program is running. Use classes that are not known at compile time. This is characterized by reflection.

So what is the effect of Java reflection?

If we had two programmers, a programmer would need to use a class written by a second programmer while writing a program, but the second programmer didn't finish the class he wrote. Then the first programmer's code can be compiled. This is not compiled by. The Java reflection mechanism allows the first programmer to compile its own code without getting a class written by a second programmer.

The reflection mechanism of Java it knows the basic structure of the class, the ability to detect the Java class structure, which we call the "self-trial" of the Java class. We've all used JCreator and eclipse. When we build an object, we call the object's methods and properties. By clicking a Point, the compilation tool automatically lists all the methods and attributes that the object can use for the user to choose from. This is the use of the principle of Java reflection, is to create the object of our discovery, self-trial.

Class Classes

to use the Java reflection mechanism correctly, you have to use the Java.lang.Class class. It is the origin of the Java reflection mechanism. When a class is loaded, the Java Virtual machine automatically generates a Class object. This class object allows us to obtain information about the methods, members, and the declarations and definitions of the construction methods that are loaded into the class object in the virtual machine.

The upper half of the section is transferred from here. use of class classes

Object oriented, everything is the object.

In the Java language, two objects that are not object-oriented: static members, normal data type classes

Static members belongs to the class
Normal data type There are packing categories to make up

Class is an object, and a class is an instance object of the Java.lang.Class class.

Any class is the strength class of class.

Class classes are created by Java Virtual machine VM.

Class class Gets an instance object that represents the use of class type (class type) class classes for instance classes

Dynamic Loading: Runtime loading Class Class.forName ("Class name"); Advantages: When used, the call does not affect the program to run.

Static record: Compile time Load class New Create object is static load class.

By instantiating multiple similar classes in a dynamically loaded method, you can create an interface that enables these similar classes to implement this interface, creating only the objects of this interface class.

The purpose of using the Class.forName () static method is to dynamically load the class. After the load is complete, the newinstance () static method under class is typically invoked to instantiate the object for manipulation.     Therefore, it is no use simply to use Class.forName () to dynamically load classes, whose ultimate goal is to instantiate objects. See the difference between newinstance () and new () in Java

Java Fetch method Information


reflection of the method

Method Reflection: Method.invoke (object, argument list) of course, the prerequisite is to get the method.

1. Get the information of the class first to get the class type of the classes to get the class object.             A A = new A (); Class C = A.getclass ();

2. Method get: Name and parameter to determine GetMethod get public method Getdelcaredmethod own declaration method

Methods M=c.getmethod ("Method name", parameter list);

The reflection operation of a method is a a1.print (argument list) that is used to invoke the method with the M object; The reflection of the method is exactly the same as the effect of this call. to understand the essence of generics by Class,method

Java collection of generics, is to prevent error input, only in the compilation phase is valid, bypassing the compilation is invalid ... For example, create a string generic ArrayList that prompts for an error when you add other types of data to it.

You can bypass compilation to add data through reflection of a method

arraylist<string> list = new arraylist<string> ();   Class C=list.getclass ();       Method M=c.getmethod ("Add", Object.class); M.invoke (list, parameter data);

But the result is that you can't use For-each to traverse or throw exception type conversion errors

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.