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 immediate superclass of the applet class?

0 Answers  


whats is mean by jndi

4 Answers   SolutionNET,


What state does a thread enter when it terminates its processing?

0 Answers  


whats is mean by object and class?

1 Answers   Saka Solutions,


What is the RMI and Socket?

0 Answers  






How do you download stubs from a Remote place?

1 Answers  


What are the sequence of steps to write pub or sub model kind of application?

0 Answers   TCS,


What is the O/P of the below Code Snippet ? And how does it imply the concept of call-by-value/call-by-reference. (Note : Pls ignore syntx errors) public class One { sop ("Into One--"); } public class Two extends One{ sop ("Into Two--"); } public class Home { One a; Two t; public static void main(argv[]) { sop ("In Home--"); sop(One.a); sop(Two.a); sop(One.t); sop(Two.t); } }

2 Answers   Wipro,


What classes of exceptions may be caught by a catch clause?

0 Answers  


Do I need to import javlang package any time? Why ?

0 Answers  


Can you write Java code for declaration of multiple inheritance in Java ?

6 Answers   Kingshir, Oracle,


What is the difference between ear, jar and war file?

0 Answers  


Categories