Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiation of class ???  RSS feed

 
senthil rajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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.
 
Sat Nar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Guys for the valuable info.

- Senthil.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • 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 ();
 
uj johansson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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,

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!