• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

what is need of private constructors in java

 
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.
 
Ranch Hand
Posts: 633
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • 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!
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please SearchFirst for lot of related queries.
 
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


 
lowercase baba
Posts: 12827
52
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.
 
Marshal
Posts: 68856
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct there, Fred.
 
Greenhorn
Posts: 18
  • 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 Windows XP Java
  • 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
 
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
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
    Bookmark Topic Watch Topic
  • New Topic