Reflection in Java Reflection is the ability of software to analyze itself. This is provided by the [Link].
reflect package and elements in Class. It allows to analyze a software component and describe its capabilities dynamically, at run time rather than at compile time. For e ample, by using reflection, we can determine what methods, constructors, and fields a class supports. The package [Link] has an interface, called Member, which defines methods that allow to get information about a field, constructor, or method of a class. There are also eight classes in this package.
The following eg illustrates a simple use of the Java reflection capabilities. It prints the constructors, fields, and methods of the class [Link]. The program uses the folling methods forName( ) -> It returns a class ob!ect for [Link]. getConstructors( ) "# return arrays of Constructor getFields( ) -> return arrays of Fields getMethods -> return arrays of Method import ![Link].$% public class Reflection&emo' ( public static void main)*tring args+,-
( try ( .lass c / .lass.for0ame)1![Link].&imension1-% 22 getting the ob!ect of class *[Link])1.onstructors31-% .onstructor constructors+, / [Link])-% 22getting the constrictors for)int i / 4% i 5 [Link]% i66( *[Link])1 1 6 constructors+i,-% 7 *[Link])1Fields31-% Field fields+, / [Link])-% for)int i / 4% i 5 [Link]% i66( *[Link])1 1 6 fields+i,-% 7 *[Link])18ethods31-% 8ethod methods+, / c.get8ethods)-% for)int i / 4% i 5 [Link]% i66- ( *[Link])1 1 6 methods+i,-% 7 7 catch)9 ception e- ( *[Link])19 ception3 1 6 e-% 7 7 7 ut!ut .onstructors3 public ![Link].&imension)![Link].&imensionpublic ![Link].&imension)int,intpublic ![Link].&imension)Fields3 public int ![Link].&[Link] public int ![Link].&[Link] 8ethods3 public int ![Link].&[Link])public boolean ![Link].&imension.e:uals)![Link].;b!ectpublic ![Link].*tring ![Link].&[Link]*tring)public void ![Link].&[Link]*ize)![Link].&imensionpublic void ![Link].&[Link]*ize)int,intpublic void ![Link].&[Link]*ize)double,doublepublic ![Link].&imension ![Link].&[Link]*ize)public double ![Link].&[Link]<idth)public double ![Link].&[Link]=eight)-
public ![Link].;b!ect ![Link].&imension>&.clone)public void ![Link].&imension>&. set*ize)![Link].&imension>&public final native ![Link]..lass ![Link].;b![Link])public final void ![Link].;b![Link])long,int- throws ![Link].Interrupted9 ception public final void ![Link].;b![Link])throws ![Link].Interrupted9 ception public final native void ![Link].;b![Link])long- throws ![Link].Interrupted9 ception public final native void ![Link].;b![Link])public final native void ![Link].;b![Link]?ll)-
"eading #ublic methods of a class The following e ample uses Java@s reflection capabilities to obtain the public methods of a class. The program begins by creating an ob!ect of class $. The getClass( ) method is applied to this ob!ect reference to returns the Class ob!ect for class $. The getDeclaredMethods( ) method returns an array of Method ob!ects that describe only the methods declared by this class. 9ach element of the methods array is then processed. The getModifiers( ) method returns an int containing flags that describe which access modifiers apply for this element. import ![Link].$% public class Reflection&emo> ( public static void main)*tring args+,- ( try ( ? a / new ?)-% .lass c / [Link])-% *[Link])1Aublic 8ethods31-% 8ethod methods+, / [Link]&eclared8ethods)-% for)int i / 4% i 5 [Link]% i66- ( int modifiers / methods+i,.get8odifiers)-% if)[Link])modifiers-- ( *[Link])1 1 6 methods+i,.get0ame)--% 7 7 7 catch)9 ception e- ( *[Link])19 ception3 1 6 e-% 7
7 7 class ? ( public void a')- ( 7 public void a>)- ( 7 protected void aB)- ( 7 private void aC)- ( 7 7 output Aublic 8ethods3 a' a> T=9 J?D? EIFR?RG