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

Are we allowed to change the transaction isolation property in middle of a transaction?

0 Answers  


What is servlet preinitialization?

3 Answers   iFlex,


How are the elements of a cardlayout organized?

0 Answers  


what are the activation groupworks?

0 Answers  


Suppose there are 3 combo box. SELECT COUNTRY SELECT STATE SELECT CITY if i select any country from country conutrylistbox values in the state will get automatically inserted with database values>> THEN on selection of state city will be inserted in city combo box If you can help then please Help me...

2 Answers   HCL,






To identify IDL language what mapping mechanism is used?

0 Answers  


What is deadlock?

2 Answers  


How we set Bean Id when we are creating web application using SpringMVC and Hibernet Integration

1 Answers  


What value does read() return when it has reached the end of a file?

0 Answers  


What is the purpose of the wait(), notify(), and notifyall() methods?

0 Answers  


How to synchronize jsp page?

1 Answers   Infotech, TCS,


Why does the option tag render selected=selected instead of just selected?

0 Answers  


Categories