What is the use of Class.forName

Answers were Sorted based on User's Feedback



What is the use of Class.forName..

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

What is the use of Class.forName..

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

What is the use of Class.forName..

Answer / p.kodanda rami reddy

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

What is the use of Class.forName..

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

What is the use of Class.forName..

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

What is the use of Class.forName..

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

What is the use of Class.forName..

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&nbsp;t&nbsp;= 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

What is the use of Class.forName..

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

What is the use of Class.forName..

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

What is the use of Class.forName..

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

Post New Answer

More Advanced Java Interview Questions

What is the use of Class.forName

20 Answers   HCL,


What is RMI and what are the services in RMI?

0 Answers  


What is a task?s priority?

1 Answers  


What method is used to create the daemon thread?

1 Answers  


What are JTA/JTS and how they used by client?

0 Answers  






Is it possible to stop the execution of a method before completion in a sessionbean?

0 Answers  


how the action can be map from jsp page to bean class in mvc1

1 Answers   Photon,


Are there books about seam?

0 Answers  


the same information whether it will connect to the database or it will be used previous information?

0 Answers  


What is the purpose of the notifyall() method?

0 Answers  


Which class is the immediate superclass of the menucomponent class?

0 Answers  


what is meant by JRMP?

0 Answers  


Categories