aspose file tools*
The moose likes Java in General and the fly likes what is need of private constructors in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is need of private constructors in java" Watch "what is need of private constructors in java" New topic
Author

what is need of private constructors in java

rajesh vasireddy
Greenhorn

Joined: Jun 06, 2009
Posts: 18
why do we create private constructors in java classes? And for creating that class object we give static factory methods. Instead why cant we dont we use public for constructor. Please help me. Thanks in advance.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

When you don't want anyone outside the class to create object of your class. like Singleton class


Life is easy because we write the source code.....
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

rajesh vass wrote:why do we create private constructors in java classes?

when you dont want a user to access a constructor from outside.it is just like making any other member of a class as private.
rajesh vass wrote:
And for creating that class object we give static factory methods. Instead why cant we dont we use public for constructor.

in this way, you can restrict the user in creating objects. for example : singleton . in other way, some people believe providing static factory method is more expressive than just giving *new* access to a user!
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

And please SearchFirst for lot of related queries.


Mohamed Sanaulla | My Blog
Selva Kumar N
Greenhorn

Joined: Aug 22, 2011
Posts: 1
Hi Rajesh Vass,

why do we create private constructors in java classes?

It is because to restrict instantiate the class(i.e the class with private constructor will not allow to create instance of that class by any other classes)

And for creating that class object we give static factory methods. Instead why cant we dont we use public for constructor?

The Constructor of this class is private so a public static method of this class is used to get the Object of the class. Because no other class can't instantiate this calss. This patter is called as Singleton pattern

for more info refer: http://www.roseindia.net/javatutorials/J2EE_singleton_pattern.shtml

Cheers!

Selva


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

Someone will correct me if I am wrong, but you could write several private constructors, and several public ones. The private ones could be used by the public ones as 'helpers'.

I am not saying this would be a smart or a good idea, but I believe it is possible.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
I think you are correct there, Fred.
Debbie Waltz
Greenhorn

Joined: May 12, 2008
Posts: 13
rajesh vass wrote:why do we create private constructors in java classes?


to make them private which means they cannot be used from outside that class.
A constructor is always required but if it is private it can be used only by another method of the same class, not even from a subclass.

If you make all of them private no class instance can exist before having used any constructor and hence you need a static factory.

rajesh vass wrote:And for creating that class object we give static factory methods.


"One advantage of static factory methods is that, unlike constructors, they have names"

a method name can better describe what sort of object you are building out of that class.

"A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked."

so you can build singletons classes or, more generally cache invariant object instances: especially if they take a lot of time to build.

"A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type."

this gives you more flexibility.
E.g. you can have an abstract class and several hidden implementations that depend on the parameters you use to build the object that provide better performances.

All this is well explained in "Effective Java 2nd ed." by Joshua Bloch were I got the quotations from.


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Debbie Waltz wrote:
All this is well explained in "Effective Java 2nd ed." by Joshua Bloch

Indeed
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
just felt like adding a few more advantages of teh factory pattern
1) You can include advanced synchronization in object creation....
2) You can have conditional object creation (if some condition is not met, you throw exception or return null instead of creating the object)
3) Do housekeeping before creating an object...not a good design.. but is needed at times
4) have pools of objects for object reuse
the list goes on
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is need of private constructors in java