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
What is enhanced loop in java?
How does queue work in java?
What is Hierarchy of exception?
How to create packages in java?
What is the mapping mechanism used by java to identify IDL language?
What is a method type?
What is the common usage of serialization?
Can singleton class be inherited in java?
What is the final keyword denotes?
How do you get the length of a string in java?
Why is java not 100% pure oops?
What is final class?
What are three ways in which a thread can enter the waiting state in java programming?
How do you create a bulleted list?
What things should be kept in mind while creating your own exceptions in java?