Skip to content

Instantly share code, notes, and snippets.

@aroranubhav
Created March 3, 2025 20:23
Show Gist options
  • Save aroranubhav/88dccb7ced94d1f6b357a0c038d2410e to your computer and use it in GitHub Desktop.
Save aroranubhav/88dccb7ced94d1f6b357a0c038d2410e to your computer and use it in GitHub Desktop.
Creating class instance via Reflection Example
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