• Post Reply Bookmark Topic Watch Topic
  • New Topic

Private constructors  RSS feed

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what is the advantage of a private constructor? I am working on some old code and the author (who is not available) has a private constructor in a class. I wont give you the exact code but this is pretty much how it goes...



What is the benefit of returning an instance of the object through a method? Does the fact that its static mean that any instances that are created can only be singletons??

Thanks,
Alan
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The private constructor means that only the class itself can create instances. The only way for another class to get an instance is via the static method, which is written to always return the same object.

Be aware, though, that that implementation isn't a thread safe singleton - if two threads call getInstance() at the same time then you can end up with two objects. It's better to drop the lazy initialisation, and just make the declaration private static MyObject instance = new MyObject();
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes we don't want a class to be instantiated at all, or to control the creation of instances, perhaps for pooling reasons or to ensure that only a specific subclass can be created, determined by us at the time the request is made. In the general case, this may be an implementation ofthe Factory Pattern or the Builder Pattern.

In this particular case, it's an implementation of the Singleton Pattern, although it's flawed for at least two reasons:

1) It uses lazy instantiation, which is completely pointless.

2) The getInstance() method is not thread safe. (Not necessarily a problem, if the author intends it only to be used in a single-threaded context.)
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

It uses lazy instantiation, which is completely pointless.



Lazy instantiation? What or why is this?

Thanks.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alan Smith wrote:
Jeff Verdegan wrote:

It uses lazy instantiation, which is completely pointless.



Lazy instantiation? What or why is this?

Thanks.


It means instantiating it when getInstance is first called, rather than at declaration.

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote: . . . It means instantiating it when getInstance is first called, rather than at declaration. . . .
If you instantiate the object before that, it is called “eager instantiation”.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!