• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Core Java

 
Sailendra Jena
Greenhorn
Posts: 9
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we can't keep constructor as private, if we will keep then what is the benefits of that to make it as private?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sailendra Jena wrote:Why we can't keep constructor as private, if we will keep then what is the benefits of that to make it as private?

Constructors can be declared as private. Please Search in the forum for related posts >> SearchFirst
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as asked before, please UseAMeaningfulSubjectLine.
 
Jacek Garlinski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sailendra Jena wrote:what is the benefits of that to make it as private?

If you want to create class only with static methods (eg. tool class), you can make constructor private to make sure, that nobody will create an instance.
 
Marcin Kwiatkowski
Ranch Hand
Posts: 32
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other use case is Factory Method pattern
 
Joydeep Ghatak
Ranch Hand
Posts: 43
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shailendra,

Please check 'Singleton Design Pattern'.

Thanks,
Joydeep
 
Sailendra Jena
Greenhorn
Posts: 9
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joydeep,

Thanks for reply and also understand that link but its not written clearly that in what senarios we required this private constructor. If we are making any class constructor as private accesss specifier then also we can create any number of object of that class then what is the benifits of that private constructor. If that class is allowing me to create any number of object after making that class constructor as private. But according to the rules of singleton object is telling that we can create only one object of that class but here we can create any number of object of this class. Like this example:
class A
{
int i = 10;

private A()
{
System.out.println("Hello World");
}

public static A getA()
{
return new A();
}
}
class Manager
{
public static void main(String args[])
{
A a1 = A.getA();
System.out.println(a1.i);
A a2 = A.getA();
System.out.println(a2.i);
}
}

In this scenarios we can create any number object of this class but according to rules of the singleton object is telling that we can create only one object of the singleton class. Then tell me what is the benifits of this private constructor.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sailendra Jena wrote:
In this scenarios we can create any number object of this class but according to rules of the singleton object is telling that we can create only one object of the singleton class. Then tell me what is the benifits of this private constructor.


You wrote your get() method wrong. A singleton's get() method does not create a new instance every time. It always returns a reference to the same instance. You can google for java singleton example to see how it's done.

In short, a private constructor lets the class author have complete control over when and how instances of that class are crated. He can elect to prevent instantiation altogether (such as for a utility class like java.lang.Math), or he can opt for a single instantiation (such as for a singleton, which your code does not properly demonstrate), or a limited number of instance, perhaps for a self-managing pool, or, before enums came around in java 1.5, for a type-safe enum pattern.
 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sailendra Jena wrote:Hi Joydeep,


In this scenarios we can create any number object of this class but according to rules of the singleton object is telling that we can create only one object of the singleton class. Then tell me what is the benifits of this private constructor.

 
Campbell Ritchie
Sheriff
Posts: 48370
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code will not work; it is not thread‑safe, and cannot be relied upon not to produce two instances. If two threads access that instance() method, they can create two instances.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic