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 is JVM and is it platform independent?

555


Can we able to pass objects as an arguments in java?

530


Can a class with private constructor be extended?

484


How does predicate work in java?

530


Describe string intern() methodology

592






Can arraylist contain null values?

563


Is linkedlist thread safe in java?

557


What is a boolean field?

523


What is meant by object oriented programming – oop?

541


What is difference between wait and notify in java?

537


What does the “final” keyword mean in front of a variable? A method? A class?

542


What is ctrl m character?

510


What is integer valueof?

637


What is http client in java?

515


According to java operator precedence, which operator is considered to be with highest precedence?

596