10 How ".class" file is loading by using Reflection API

API stands for Application Program Interface.
It contain collection of classes and interfaces and enum and annotation.
With the help of all of this we are highlighting some operations.

In market we have different api like
For Database --->ojdbc6.jar--> we have jdbc-api
For WebApplication --> Servlet-api

Like this by using reflection api we can develop reflection oriented operations.

Reflection Oriented Operation :
Suppose if you have file A.java and you are compiling it and getting A.class again it you want Source code i.e A.java then doing this operation is called as Reflection API.
For this we have already one command javap.

 To develop reflection we have one method
forName(String Argument) which is a static factory method loading byte code of a particular class.
This method is predefined in java.lang.Class 
This method is having one string argument which is always required class name.  

For ex:
java.lang.Class.cls=java.lang.Class.forName("oracle.jdbcdriver.OracleDriver");

By writing like this forName() method automatically load OracleDriver.class file from secondary memory to primary memory.
OracleDriver.class contains Byte Code with the help of this byte code forName() method will create one java.lang.Class referenced variable. Now this class referenced variable contains Byte Code the byte code is related to which class OracleDriver.class .

Now Suppose we have two file Demo.java and Loading.java

Demo.java

class Demo{
static{
System.out.println("Demo Static Block");
}

}

Loading.java

class Loading{
public static void main(String[] r)
throws ClassNotFoundException{
Class.forName("Demo");
}
}



By writhing Like this forName() method itself loading Demo.class file from secondary memory to primary memory automatically Demo.class file is going to be loading, initialize and executing.
Previous
Next Post »