aspose file tools*
The moose likes Beginning Java and the fly likes Core Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Core Java" Watch "Core Java" New topic
Author

Core Java

Sailendra Jena
Greenhorn

Joined: Aug 30, 2010
Posts: 9

Why we can't keep constructor as private, if we will keep then what is the benefits of that to make it as private?


sailendra.n.jena@hotmail.com
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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


Mohamed Sanaulla | My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

And as asked before, please UseAMeaningfulSubjectLine.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jacek Garlinski
Greenhorn

Joined: Aug 08, 2011
Posts: 8
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.


Please correct my English.
Marcin Kwiatkowski
Ranch Hand

Joined: Aug 06, 2007
Posts: 32

Other use case is Factory Method pattern
Joydeep Ghatak
Ranch Hand

Joined: Sep 30, 2009
Posts: 41

Hi Shailendra,

Please check 'Singleton Design Pattern'.

Thanks,
Joydeep


“Men are only as good as their technical development allows them to be.”
Sailendra Jena
Greenhorn

Joined: Aug 30, 2010
Posts: 9

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Aug 29, 2012
Posts: 100
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

Joined: Oct 13, 2005
Posts: 38865
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Core Java