Skip to content

Instantly share code, notes, and snippets.

@evrentan
Created December 5, 2021 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrentan/2a2ce63d719f97041839123cce2f0254 to your computer and use it in GitHub Desktop.
Save evrentan/2a2ce63d719f97041839123cce2f0254 to your computer and use it in GitHub Desktop.
Reflection Example of Getting an Instance of a Class
package evrentan.examples;
public class ReflectionGetClassInstanceTest {
public static void main(String[] args) throws ClassNotFoundException {
//Get class instance by using forName() method
Class reflectionTestClassWithForName = Class.forName("evrentan.examples.ReflectionGetClassInstanceTest");
System.out.println(reflectionTestClassWithForName.getName());
//Get class instance by using getClass() method
ReflectionGetClassInstanceTest reflectionGetClassInstanceTest = new ReflectionGetClassInstanceTest();
Class reflectionTestClassWithGetClass = reflectionGetClassInstanceTest.getClass();
System.out.println(reflectionTestClassWithGetClass.getName());
//Get class instance by class syntax
Class reflectionTestClassWithClass = ReflectionGetClassInstanceTest.class;
System.out.println(reflectionTestClassWithClass.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment