File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes different ways of instantiating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "different ways of instantiating" Watch "different ways of instantiating" New topic

different ways of instantiating

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 241
hi guys

what is the difference between

creating an instance

using new operator,using class.forName(), using class.newInstance()

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Generally, you use the "new" operator to create an instance. For example:

Object theObject = new MyClass();

But you can instantiate an object from the class name as a String For example:

Class theClassObject = Class.forName("MyClass");

The above is used to get an object representing the class that you want to instantiate. Then you create your object as follows:

Object theObject = theClassObject.newInstance();

This is a more round-about way to instantiate an object but is more dynamic because the class of the object being instantiated can be something that is not known at compiile-time.

[ May 23, 2007: Message edited by: Kaydell Leavitt ]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I want to add that you can also instantiate objects by:

cloning or deserializing

Are there any other ways to instantiate an object besides these 4 ways?

Amirtharaj Chinnaraj
Ranch Hand

Joined: Sep 28, 2006
Posts: 241
kaydell thanks for your reply

i need an explaination how this instanting

methods are treat by jvm .i thought

new operator will create object at compile time

but class.forName() and class.newInstace will create

object at runtime but i need a way to examine it

iam looking for that

looking for your replies
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

I believe that usually, almost always, the best way to instantiate an object is by using the "new" operator. The "new" operator instantiates an object every time that its line of code is executed.

Here's a real-world example of using Class.forName(). This code comes from an example of how to use a JDBC database engine, using a driver.


Here, instantiation is dynamic because the parameter is a String that can be any JDBC database engine driver. In theory, you could switch database engines merely by changing the value of the parameter.

I had a real hard time getting the dynamic instantiation working in the one line of code above.

After spending a great deal of time trying to get the example code working, thinking that my database engine wasn't installed properly, I tried the following line of code:

new org.apache.derby.jdbc.EmbeddedDriver();

To use the "new" operator, you can't even compile unless the class that you're instantiating is findable.

The above line of code above worked. It's much simpler.

Once I got the "new" operator working, then I could turn my attention toward generalizing the code by making the class name to be instantiated a parameter as in the original example.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

objects are never created at compile time. How could they be? I can compile my code on machine 'A', then send the class files over the sneakernet to machine 'B'. That machine can run the code, without ever knowing anything about 'A'.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henrique Ordine
Ranch Hand

Joined: Sep 03, 2004
Posts: 127

I think the compilation difference you're looking for is this:
With class.forName(String).newInstance() your class doesn't actually have to exist at compile time. However, if the jre doesn't find it at run time, your code will throw a NoClassDefFoundException.
With the new operator, your class will not even compile, if it can not be found.

Another difference is that by using new you can call different constructors in the class you're instantiating. And by using Class.newInstance() you're only able to call the no-args constructor of that class.
However, there is a way to instantiate objects dinamically and by calling any constructor of that object's class. For example, if you want to call a (String) constructor:

J2EE Architect/Developer
I agree. Here's the link:
subject: different ways of instantiating
It's not a secret anymore!