It's not a secret anymore!
The moose likes Java in General and the fly likes Class.forName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class.forName()" Watch "Class.forName()" New topic


Suresh Appagundi
Ranch Hand

Joined: Sep 22, 2003
Posts: 44
What makes the difference between getting the instance of the Particular
class using
Class.forName() {what is dynamic loading? how it useful?}
new keyword?
thanks in advance
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
as far as Class.forName() is concerned it is used to load a class at runtime. i.e. at the time of compilation we don't know which class should be loaded in memory but through programming we decide which class is to be loaded then we say
this will load Foo into memory when this line is reached during runtime.
now if you want to make an instance of this at that time then you can call

now this will return an object of Class "Object" and you can typecast it to your class and then use it..
but new is used to create an object of the class when that class is already loaded in the memory. we use new for those classes which we have added in bytecode using import statement.
i hope i have answered your query.. if any doubt please revert back

****************************<br />In 24 hrs Earth rotates once on its Axis.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Some of the examples of dynamic classloading are JDBC drivers, Servlet instance, EJB instances etc.

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the intermediate format...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jacquie Barker
Ranch Hand

Joined: Dec 20, 2000
Posts: 201
A class's byte code is loaded by the Java Virtual Machine at run time the first time an instance of that object is created using the "new" operator; but, if you want to load a class WITHOUT having to create an instance of it, Class.forName() does the trick!

Author of Beginning Java Objects, Beginning C# Objects, and Taming the Technology Tidal Wave
I agree. Here's the link:
subject: Class.forName()
It's not a secret anymore!