what is class.forname() and how it will be useful ?

Answers were Sorted based on User's Feedback



what is class.forname() and how it will be useful ?..

Answer / amit singh

it is useful when you don't know about the class as compile
time you must to use it you pass the class name as a string
and
its a part of reflection api
but you used it when you connect through db
and many more you should do when you don't know the class
at compile time
and also above which mention by pradip right
amitsingh200@gmail.com
amit09mca(scjp 1.5 and manymore)

Is This Answer Correct ?    5 Yes 0 No

what is class.forname() and how it will be useful ?..

Answer / 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

what is class.forname() and how it will be useful ?..

Answer / keerthi

In java we are using class.forName("classname")in jdbc
for creating instance of driverclass at the same to
registering with drivermanager.

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More Core Java Interview Questions

What do negative exponents mean?

0 Answers  


Explain about features of local inner class?

0 Answers  


Question 7 [8] Consider the following class and answer the questions below it: public class StackWithGuard extends Stack { public StackWithGuard(int size) { super(size); } synchronized public boolean isEmpty() { return super.isEmpty(); } synchronized public boolean isFull() { return super.isFull(); } synchronized public int getSize() { return super.getSize(); } synchronized public void push(Object obj) { try { while (isFull()) { wait(); } } catch (InterruptedException e) {} super.push(obj); COS2144/102 11 notify(); } synchronized public Object pop() { try { while (isEmpty()) { wait(); } } catch (InterruptedException e) {} Object result = super.pop(); notify(); return result; } public static void main(String args[]) { StackWithGuard stack = new StackWithGuard(5); new Producer(stack, 15).start(); new Consumer(stack, 15).start(); } } Note: The Stack class is provided in the Appendix. Note also: The following questions all refer to the pop() method of the StackWithGuard class given above. 7.1 What does the synchronized keyword ensure for this method? (2) 7.2 Why is a while loop used to test whether the stack is empty? In other words, why wouldn't the following if statement be sufficient? if (isEmpty()) { wait(); } (2) 7.3 Why is the result of popping (provided by the inherited pop() method) stored in a temporary variable? In other words, why wouldn't the following statement be sufficient? return super.pop(); (2) 7.4 Why is the while loop placed in a try-catch structure? (2) Appendix The LinkedQueue class: public class LinkedQueue implements Queue { private Node first, last; private int count; public LinkedQueue() { first = last = null; count =0; } public int size() { return count; } public boolean isEmpty() { return (count == 0); 12 } public void enqueue(Object o) { Node node = new Node(); node.element = o; node.next = null; node.prev = last; if (last != null){ last.next = node; } else { last = first = node; } last = node; count++; } public void dequeue() { if ((first!= null) & (first.next!=null)) { first = first.next; first.prev = null; count--; } else { first = last = null; count--; } } public Object front() { return first; } } class Node { Object element; Node next, prev; } The Stack class: public class Stack { protected Object rep[]; protected int top = -1; protected int size = 0; protected int count = 0; public Stack(int size) { if (size > 0) { this.size = size; rep = new Object[size]; } } public boolean isFull() { return (count == size); } public boolean isEmpty() { return (count == 0); } public int getSize() { return size; } public void push(Object e) { if (e != null && !isFull()) { COS2144/102 13 top++; rep[top] = e; count ++; } } public Object pop() { Object result = null; if (!isEmpty()) { result = rep[top]; top--; count--; } return result; } }

0 Answers  


What is the difference between import java.util.date and java .util?

0 Answers  


What is java in layman terms?

0 Answers  






Can substring create new object?

0 Answers  


why we r not use pointers in corejava?

2 Answers   Samsung,


What is HashTable?

4 Answers  


What exactly is a .class file?

0 Answers  


Which object oriented concept is achieved by using overloading and overriding?

0 Answers  


What is merge sort in java?

0 Answers  


What is bom encoding?

0 Answers  


Categories