How to create an instance of a class without using "new"
operator? Plz help me out properly.Thank u.

Answer Posted / satchidananda.lanka

Dynamic loading is a technique for programmatically
invoking the functions of a class loader at run time.

Let us look at how to load classes dynamically.

Class.forName (String className); //static method which
returns a Class
The above static method returns the class object associated
with the class name.
The string className can be supplied dynamically at run
time.
Once the class is dynamically loaded the following method
returns an instance of the loaded class. It’s just like
creating a class object with no
arguments.
class.newInstance (); //A non-static method, which creates
an instance of a
//class (i.e. creates an object).

Example as shown below ....

Test test = null ;
String myClassName = "com.test.DynamicLoadingClass" ;
Class testClass = Class.forName(myClassName) ;
test = (Test) testClass.newInstance();

Is This Answer Correct ?    10 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are void pointers?

969


what is meant by HQL?

821


enlist some features of jdk.

737


What is the benefit of using enum to declare a constant?

852


What is public/private protected in java?

771


What is difference between final and immutable?

759


Explain about instanceof operator in java?

795


Can you override private or static method in java?

749


What is Hierarchy of exception?

836


how can you take care of mutual exclusion using java threads? : Java thread

798


What is flag in java?

726


do I need to use synchronized on setvalue(int)? : Java thread

747


What is a protected class in java?

749


How many arguments can be passed to main ()?

732


What is oop principle in java?

731