• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton object  RSS feed

 
ssasi kumar
Greenhorn
Posts: 26
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have one doubt what is difference between getInstance() and new Class()?

For Example :



Thanks
Sasikumar
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this code even compile?
For i strongly doubt that it will,
by the way, what do you think is the difference ?
 
ssasi kumar
Greenhorn
Posts: 26
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I asking what is difference between using Test.getInstance() or new Test() creating object?
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is _ui..??
Where is it defined?

"new Class()" Class is a predefined class in java refer to doc
 
ssasi kumar
Greenhorn
Posts: 26
Chrome Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ssasi kumar wrote:
what is difference between using Test.getInstance() or new Test() creating object?


I think you should try it out by writing some println statements in default constructor.
But basically i dont see any difference.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please compile the code and tell what is happening..?

And please explain the below line.
ssasi kumar wrote:

 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ssasi kumar wrote:hi,

I asking what is difference between using Test.getInstance() or new Test() creating object?

Test.getInstance is a method and Test() is a constructor .. that's the basic difference
And there is no such method as Class.getInstance().. you are obviously confusing it with Class.getClass().newInstance() method which creates a new instance of the class represented by the object. It throws a InstatiationException. By using this method the Class is instantiated as if by a new expression with an empty argument list.
while the new keyword actually creates a new object



 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So based on my above description i would presume that you are baffled with is "why use newInstance()" when the new keyword is also doing the same thing as to create a new object. Well that's why the title of your OP, "Singelton Class"
Suppose you have created a class that pretty much does nothing. And you would not want to create multiple objects out of it so what you do is you restrict the instantiation to just one object.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't like that design, though I think it may work correctly in Java5+. Joshua Bloch gives a better design which is a bit like this:-I have marked the class final out of courtesy only.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Joshua Bloch gives a better design which is a bit like this:

@ssasi - But before you start littering your programs with them, you might want to read THIS.

Winston
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a far better why not use singletons link than some I have seen Thank you.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't like that design, though I think it may work correctly in Java5+. Joshua Bloch gives a better design which is a bit like this:


Indeed, there's absolutely no reason for lazy instantiation of a singleton (ignoring for the moment the question of whether we even really want a singleton at all in the first place).
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> So based on my above description i would presume that you are baffled with is "why use newInstance()"
> when the new keyword is also doing the same thing as to create a new object.


With "new ..." the exact type must be known at compile time. Whereas with newInstance() it can be known at runtime.


 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ssasi kumar wrote:Hi everyone,

I have one doubt what is difference between getInstance() and new Class()?



Do you mean your version of getInstance() method and your 'new Test()' assignment?

new Test() constructs an object by invoking the no argument constructor you've coded in your class.
What your getInstance() method is doing is checking if you have a pre-constructed Test object already. If so, it is returning you the reference of that already constructed object. If not, it constructs a new object by invoking your no argument constructor and returns that reference.

getInstance() method is generally coded to get the reference of an already constructed object. Whether the class that has getInstance() method is a singleton class or not depends on many other factors.

getInstance() is a naming convention for static methods that return you an instance. In some cases, such static methods along with private constructors enforce the singleton behavior in a class.

Chan.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!