


Here is a brief rundown of what might throw an exception: The method declares all of them in its signatures. Most of the methods in this short invokeMain method may throw various exceptions.

In this case, our main method is a void method, so we ignore the return type. The invoke() method returns an Object that will contain the result that the reflected method returns. However, we must cast the parameter to Object to indicate that the array is the parameter, and not that the parameters are in the array. Since we only have a single parameter we pass it as the second argument. Since we are invoking a static method and not an instance method, we pass null as the instance argument. This method's first parameter is the instance to invoke on, and the remaining parameters are for the invokee's parameters. The getMethod method has a return type of we store the result in a local variable named mainMethod.įinally, we invoke the method by calling the invoke() method of the Method instance. main accepts one parameter ( String args) so we add a single Class element representing the String. We then add a Class variable for each of the method parameters. The method name is trivial: we want to invoke the main method, so we pass in the name "main". It takes a variable number of parameters: the method name is the first parameter and the remaining parameters are the types of each of main's parameters. The getMethod() method is defined for the Class class. The third statement at line 11 performs a reflection operation on the Distance class. The second statement at line 10 simply creates a String array with the four command line arguments we wish to pass to the main method of the Distance class. We store the class object in the local variable distanceClass its type is Class. In this case, we are loading the class "Distance" from the default package. Class that results from loading the class. The forName() method will load a Java class and return an instance of java.lang. The first statement at line 9 is an example of dynamic class loading. Let's explore what the invokeMain method is doing. Reflection allows a Java program to work with classes even though the classes are not known when the program was written. The name of the class to execute is a runtime value. However, the main Java runtime does not know about the Distance class.

One way to understand how reflection works is to use reflection to model how the Java Runtime Environment (JRE) loads and executes a class. One of the most important aspects of reflection is dynamic class loading.Įxample: Invoking a main method Reflection is most useful for performing dynamic operations with Java - operations that are not hard-coded into a source program, but that are determined at run time. Most of the Java classes that support reflection are in the package. The Java runtime provides the corresponding classes for reflection. You can use these reflection objects to access fields, invoke methods, or instantiate instances, all without having compile-time dependencies on those features. Class, which in turn has methods that return the fields, methods, constructors, superclass, and other properties of that class. Object, which returns an object with the runtime representation of that object's class this object is an instance of the java.lang. In other words, there are object-based mirrors that 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.Įach object instance has a getClass() method, inherited from java.lang. 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.
