Reflection mechanism in Java

Source: Internet
Author: User
Tags constructor reflection

Reflection, reflection, listening to its name like a mirror, you can see you can see each other's parts. This is a very important feature in the Java language. Here is a presentation from Sun's website about reflection:

Reflection is a feature in the Java programming language. It allows an executing Java programming to examine or "introspect" upon itself, and manipulate internal properties of the prog Ram. For example, it's possible for a Java class to obtain the names of all its and display them.

The ability to examine and manipulate a Java class from within itself may not sound as much as very Ng languages This feature simply doesn ' t exist. For example, there are no way in a Pascal, C, or C + + program to obtain information about the functions defined Program.

One tangible use of the reflection is in JavaBeans, where software components can be manipulated visually via a builder tool. The tool uses reflection to obtain the properties of Java components (classes) as they are dynamically.

So the explanation is that reflection is a feature of the Java language that allows a program to examine itself at runtime (when it is not compiled) and to manipulate the internal members. For example, it allows a Java class to acquire all of his member variables and methods and display them. This can be specific and we don't see it very often, but in other things like C or C + +, there is no such feature. A common example is that in JavaBean, some components can be manipulated through a constructor. This constructor is the property of the class in Java that is used to get the reflection in the dynamic load.

Forward-Reflection: Class-Type class class

Java has a class is very special, is class classes, a lot of friends in the writing program when the use of such as apple.class to view the type of information, we can understand it as encapsulated the class information, a lot of explanation that class classes do not have constructors, in fact, there are, But its construction method is private (constructor and private??) Yes, this is to prevent developers from creating instances of class classes themselves.

If we get the type information of a class, we can use reflection to get its various members and methods. (Note: Class starts more generic services since the JDK1.5 version) so how do we get a type of information? Let's say we have a role class:

Package Yui; /** * A base class Having some attributes and methods * @author Octobershiner * @since 3 * * p  
     Ublic class Role {private String name;  
           
     Private String type;  
     Constructors Public role () {System.out.println ("constructor role () is invoking");  
         }//Private builder Private role (String name) {this.name = name;  
     System.out.println ("Constructor role (String name) is invoking.");  
     //get and set method public String GetName () {return name;  
     public void SetName (String name) {this.name = name;  
     Public String GetType () {return type;  
     public void SetType (String type) {this.type = type;  
         }//override the ToString method to show the class @Override public String toString () {Return ' This is a role called ' +this.name; }  
           
 }

In the absence of object instances, there are two main ways.

Two ways to get class types  
class cls1 = Role.class;  
Class cls2 = Class.forName ("Yui.") Role ");

Note In the second way, the parameters in forname must be the complete class name (package name + class name), and this method needs to catch exceptions. Now you can create an instance of a role class by using the Newinstance method of class, which is equivalent to calling the default constructor of a class. Cls1

Object o = cls1.newinstance (); Create an instance  
 //object O1 = new Role ();   Equivalent to the above method

This creates an object, and the disadvantage is that we can only take advantage of the default constructor, because class newinstance is not a parameter, and then the newinstance of acceptable arguments, and second, if the constructor of the class is private, such as class, We still cannot instantiate its 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.