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
Can a class with private constructor be extended?
Which of the following classes will have more memory allocated?
Is main a keyword in java?
What class allows you to read objects directly from a stream in java programming?
What is ternary operator?
Do we need to manually write Copy Constructor?
Is java call by value?
Can a static block throw exception?
Why we use protected in java?
What is bufferedwriter?
Is there a sort function in java?
Why java is used everywhere?
how does the run() method in runnable work? : Java thread
define polymorphism in java
What does java stand for?