how to instantiate object of class whoes class is not known at compile time?
posted 12 years ago
Hi frnds, this is one interview question what i faced
how can u instantiate object whoes class name is not know at compile time? means A a=new A(); is not valid ans for this.. i think it is asking for method ,but which method that i dont know plz help me thank you in advance
You can instantiate classes dynamically using reflection. Reflection also allows you to inspect classes: lookup up a class's fields and methods and access/call them for specific objects. A good portion of the API is accessible on java.lang.Class, including newInstance() as Mani pointed out.
I find it very useful for framework and infrastructure code, less so for general day-to-day use. However, it's one of the coolest features of Java in my eyes and very fun to play with.