Introduction to the Java Reflection API

Source: Internet
Author: User
Tags modifier reflection

The Java Reflection API provides insight into the classes, interfaces, and objects in the JVM. Developers often use APIs to accomplish the following tasks, which explains why they always use development tools, such as debugger and integrated Development environments (IDEs):

· Determines the class of an object.

· Get information about a class of modifiers, fields, methods, constructors, etc.

· Gets the constants and method declarations about an interface.

· Creates a instance of a class whose name is not known until run time, and is available at design time or as a running parameter.

· Gets and sets the field value of an object, even if the name of the field is not known until the Run-time program.

· Calls a method on an object, even if the method is not known until run time.

The specific use of reflection is in JavaBeans, where you can manipulate software components with a build tool. Tools use reflection to get properties of Java components (classes) because they are loaded dynamically.

Use reflection to retrieve class Behavior

To understand how you use reflection to determine a class behavior, consider a simple example of the following employee class:

public class Employee 
{public
String empnum;
Public String EmpName;
Public Employee ()
{This
("1", "King");
Public
Employee (string empnum, String empname)
{
empnum = empnum;
EmpName = EmpName;
Public
String toString ()
{return
"Employee details:empnumber:" + Empnum + ",
empname:" +  Empnum;
}
}
import java.lang.reflect.Modifier;
public class Analyzeclass
{public
static void Main (string[] args)
{
Employee employee = new E  Mployee ();
Class Klass = Employee.getclass ();
System.out.println ("Class name:" + klass.getname ());
System.out.println (
"Class Super Class:" + Klass.getsuperclass ());
int mods = Klass.getmodifiers ();
System.out.println (
"Class is public:" + modifier.ispublic (mods));
System.out.println (
"Class is final:" + modifier.isfinal (mods));
System.out.println (
"Class is Abstract:" + modifier.isabstract (mods));
}

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.