• 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

Instantiation of class ???

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frnds,

I have seen two kinds of class instantiation. One is,

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

and other one is,

Myclass m = new Myclass();

- What is the difference between the above two type of object creation?
- Can i use the first type for instantiating Myclass??

Thanks in Adnavace,
Senthil.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Senthil,

Myclass m = new Myclass();

This creates a new object of class MyClass and assigns the reference to this object to the variable m.

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

This looks like you have see the code which implements Singleton patternn.

public class DocumentBuilderFactory
{
private static _instance = new DocumentBuilderFactory ();

private DocumentBuilderFactory ();

public static DocumentBuilderFactory getInstance()
{
return _instance;
}
}

You can get the instance of the DocumentBuilderFactory by calling

DocumentBuilderFactory.getInstance()

Here DocumentBuilderFactory is a class and not an object since you are accessing a static method which inturn returns the instance.

So to instantiate a class use class <var> = new class()

Regards,
Sathya
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also,

the "new" way of doing it is built in to the language, and ultimately the only way of normally creating an instance (ignoring reflection for now.)

The factory method is based on the factory pattern, and internally uses the above way of instantiating.
 
senthil rajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys for the valuable info.

- Senthil.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the risk of going too advanced for this forum ...

Another subtle difference is that when you write "new MyClass()" you know exactly what class will be instantiated and you must have access to MyClass at compile time. When you get an instance from a "factory method" like newInstance() you know you will get something compatible with DocumentBuilderFactory but you don't have to know exactly what you'll get. Something deep in the configuration of the system might plug in different classes that extend DocumentBuilderFactory on different platforms or for different days of the week. This is far more flexible than hard-coding your knowledge of MyClass.

If that sparks your interest but raises more questions than answers, wander down to the OO, UML, etc forum and ask away.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public class DocumentBuilderFactory
{
private static _instance = new DocumentBuilderFactory ();

private DocumentBuilderFactory ();

public static DocumentBuilderFactory getInstance()
{
return _instance;
}
}



There are few minor mistakes that I like to pointout.


private DocumentBuilderFactory ();



This is actually a private constructor to prevent direct instantiation. I should be like

private DocumentBuilderFactory () { }


private static _instance = new DocumentBuilderFactory ();



You need to specify an identifier here. Like

private static DocumentBuilderFactory _instance = new DocumentBuilderFactory ();
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by senthil rajan:
- What is the difference between the above two type of object creation?
- Can i use the first type for instantiating Myclass??



You can use the first type. Actually using new is only built in language mechanism to create objects in Java (actually there's also another way called cloning but that gives you a copy of an already existing object).

The second type also uses new but this is hidden in a method. In principle some variation of this,

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic