Tuesday, October 7, 2008

JAVA PROGRAMMING REFLECTION

Reflection is a new concept in Java, and did not exist in classical compiled languages like C, and C++. The idea is to discover an object's attributes and its methods progrematically.


Reflection is the mechanism by which Java exposes the features of a class during runtime, allowing Java programs to enumerate and access a class' methods, fields, and constructors as objects. In other words, there are object based mirrors which reflect the Java object model, and you can use these objects to access an object's features using runtime API constructs instead of compile time language constructs.

Each object instance has a getClass() method, inherited from java.lang.Object, which returns an object with the runtime representation of that object's class; this object is an instance of the java.lang.Class This object in turn has methods which return the fields, methods, constructors, superclass, and other properties of that class.

You can use these reflection objects to access fields, invoke methods, or instantiate instances, all without having compile time dependencies on those features. The Java runtime provides the corresponding classes for reflection. Most of the Java classes which support reflection are in the java.lang.reflect package.

Reflection is most useful for performing dynamic operations with Java - operations which are not hard coded into a source program but which are determined at run time. One of the most important aspects of reflection is dynamic class loading.

No comments: