What is the use of Class.forName
Answers were Sorted based on User's Feedback
Answer / vivek katta
Class.forName will basically load a class. As part of class loading, static variables
will be initialized and static blocks present in the class will be executed.
This is the concept used in JDBC to register driver(s). A static block in the driver
class gets executed which inturn registers the JDBC driver with the connection
manager.
| Is This Answer Correct ? | 66 Yes | 2 No |
Answer / raghavendra desai
when the Class.forName("oracle.jdbc.driver.OracleDriver) is
executed, the driver calss will be loaded if it was not
loaded earlier. As soon as the class is loaded the static
block will be executed. In the static block the JDBC driver
vendors are responsible for providing the static block in
the driver class. In static block they would have been
written the similar kind of code.
public class oracle.jdbc.driver.OracleDriver implements
Driver
{
static
{
Driver drv= new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(drv)
-----
-----
some other code
}
}
| Is This Answer Correct ? | 36 Yes | 3 No |
the use of class.forName is Loading the driver
class is a class and forName is a static method.
accepting a string argument represented as the driver.
| Is This Answer Correct ? | 44 Yes | 16 No |
Answer / priyanjan
The particular operation of forName method,a static method
of class Class, is to register the driver and load it into
memory.
| Is This Answer Correct ? | 24 Yes | 11 No |
Answer / amit beriwal
forName
public static Class forName(String className)
throws ClassNotFoundException
Returns the Class object associated with the class or
interface with the given string name. Invoking this method
is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of
the current class.
For example, the following code fragment returns the
runtime Class descriptor for the class named java.lang.Thread:
Class t = Class.forName("java.lang.Thread")
A call to forName("X") causes the class named X to be
initialized.
Parameters:
className - the fully qualified name of the desired
class.
Returns:
the Class object for the class with the specified name.
Throws:
LinkageError - if the linkage fails
ExceptionInInitializerError - if the initialization
provoked by this method fails
ClassNotFoundException - if the class cannot be located
Source:-http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#forName%28java.lang.String%29
| Is This Answer Correct ? | 8 Yes | 0 No |
Answer / n.sriram
class.forName() is used to load the class at runTime. when
we could not decide which class to load.The Method just
loads the class into memory and when required we can
instantiated the class using Class.forName.newInstance()
| Is This Answer Correct ? | 15 Yes | 9 No |
Answer / naresh
/**
* Returns the <code>Class</code> object associated with
the class or
* interface with the given string name. Invoking this
method is
* equivalent to:
*
* <blockquote><pre>
* Class.forName(className, true, currentLoader)
* </pre></blockquote>
*
* where <code>currentLoader</code> denotes the defining
class loader of
* the current class.
*
* <p> For example, the following code fragment returns the
* runtime <code>Class</code> descriptor for the class named
* <code>java.lang.Thread</code>:
*
* <blockquote><pre>
* Class t = Class.forName("java.lang.Thread")
* </pre></blockquote>
* <p>
* A call to <tt>forName("X")</tt> causes the class named
* <tt>X</tt> to be initialized.
*
* @param className the fully qualified name of
the desired class.
* @return the <code>Class</code> object for the
class with the
* specified name.
* @exception LinkageError if the linkage fails
* @exception ExceptionInInitializerError if the
initialization provoked
* by this method fails
* @exception ClassNotFoundException if the class cannot
be located
*/
public static Class<?> forName(String className)
throws ClassNotFoundException {
return forName0(className, true,
ClassLoader.getCallerClassLoader());
}
Source: Source code copied from Class class of JDK 1.6.
Hope this would be useful.
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / shahin ali ansari
we know there are two type of class loading in memory first
one static loading and second one is dynamic loading,static
block load in memory before main.This is the concept used
in JDBC to register driver(s). A static block in the driver
class gets executed which inturn registers the JDBC driver
with the connection
manager.
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / mamitha
It loads the neccessary driver at run time and provide the features based on the database connecting. forName() is the static method and class is a class.
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / chaitanya
Class.for name is a Specially designed mechanism to load the drive class from secondary memory to the primary memory DYNAMICALLY.i;e we need create any object it will dynamically loads the class creates the object registers the object with Driver
| Is This Answer Correct ? | 0 Yes | 0 No |
In HashTable I am storing null value..then what is the error it will show
1) which method of the RequestDispatcher cannot be called once the output is sent to the client? a. forward b. include c. both a&b 2) which interface should an object implement to get notified of changes to the list of active sessions in a web application? a. HttpSessionListener b. HttpSessionActivationListener c. HttpSessionAttributeLIstener 3) A user can select multiple locations from a list box on an HTML form, which of the following methods can be used to retrieve all the selected location? a. getParameter() b. getParameterValues() c. getParamValues() 4) which of the following methods should be used to send character text to the client? a. ServletResponse.getWriter() b. ServletResponse.getOutputStream() c. ServletResponse.getOut() 5) which implicit object is always available in a JSP page? a. exception b. session c. out 6) which inclusion mechanism doesn't include the source of the page, rather the output of the page. a. include directive b. jsp:include action c. Bothe a& b 7) which attribute of the page controls whether a page participates in session or not? a. session-allowed b. session c. isSession
What are the various thread priorities?
When you will synchronize your code?
What state does a thread enter when it terminates its processing?
Explain about thread synchronization inside a monitor?
Difference between loadclass and class.forname?
what is the use of Object Factories?
How is serialization used generally ?
What are the services in RMI ?
Difference between hashmap and hashtable?
How do you maintain a stateful session object across the session