what is class.forname() and how it will be useful ?
Answer Posted / pradip
class.forname() is used to create an instance of a class on
the basis of the name in string format instead of doing new
Class. One of its use is in cases where you have some
generic (common) logic to call methods of some classes just
on the basis of the names of the class and the method. The
example of reflection shown below uses the logic.
Eg:
performAction(String className, String methodName) {
Class class = Class.forName(className);
MethodUtils.invokeMethod(class.newInstance, methodName,
null);
}
| Is This Answer Correct ? | 3 Yes | 0 No |
Post New Answer View All Answers
What is a constructor, constructor overloading in java?
What is polymorphism and what are the types of it?
If A Class Is Declared Without Any Access Modifiers, Where May The Class Be Accessed?
Why is String immutable?
Add a value x to array from index l to r where 0 <= l <= r <= n-1
What is volatile data type?
How do generics work?
When do we use synchronized methods in java?
What is class variable java?
What classes of exceptions may be thrown by a throw statement?
How do you use compareto method?
What is java lang string?
Is nullpointerexception checked or unchecked?
Is it safe to install java on my computer?
Can you have two constructors in java?