Created
December 5, 2021 07:17
-
-
Save evrentan/2a2ce63d719f97041839123cce2f0254 to your computer and use it in GitHub Desktop.
Reflection Example of Getting an Instance of a Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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