Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is need of private constructors in java

 
rajesh vasireddy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you don't want anyone outside the class to create object of your class. like Singleton class
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please SearchFirst for lot of related queries.
 
Selva Kumar N
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12199
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50246
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct there, Fred.
 
Jack Dwaltz
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Debbie Waltz wrote:
All this is well explained in "Effective Java 2nd ed." by Joshua Bloch

Indeed
 
Gaurav Raje
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic