• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

different ways of instantiating

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys

what is the difference between

creating an instance

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

regards
amir
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Kaydell
[ May 23, 2007: Message edited by: Kaydell Leavitt ]
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Kaydell
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
amir
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();

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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amirtharaj,

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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic