Created
March 3, 2025 20:23
-
-
Save aroranubhav/88dccb7ced94d1f6b357a0c038d2410e to your computer and use it in GitHub Desktop.
Creating class instance via Reflection Example
This file contains hidden or 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
| class SomeClass { | |
| fun doTask() { | |
| for (i in 1..10) { | |
| println(i) | |
| } | |
| } | |
| } | |
| fun <T> createClassInstance(someClass: Class<T>): T{ | |
| return someClass.getDeclaredConstructor().newInstance() | |
| } | |
| fun main() { | |
| val classInstance = createClassInstance(SomeClass::class.java) | |
| classInstance.doTask() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment