D.3. Dynamic instantiation
You will undoubtedly be familiar with the standard technique for instantiating new classes in Java, which is
MyClass myObject = new MyClass();
This causes MyClass to be initialized, and an instance of this class to be created. Java also provides a mechanism for instantiating classes dynamically—that is, where information about the class is provided at runtime. Using the techniques described above, we can get a Class reference to a class identified only by its name, and then instantiate it by calling newInstance() on the Class. For example:
Class class = Class.forName(”com.something.MyClass”); com. something.MyClass myObject = class.newInstance();
This will cause the class’s default constructor to be executed (if ...
Get Applied Enterprise JavaBeans™ Technology now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.