Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

private Constructors

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
what is the real time example for the usage of private constructors
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Apna,

Welcome to the JavaRanch!

private constructor means you dont want to allow any code ouside your class to instantiate your class.

Private constructor can be used when you don't want to allow direct instantiation of your class and to restrict the world to call your factory method to instantiate your class. That factory method is public and static;

 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private constructors are also used in the singleton pattern
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
the real time example of private constructors usage is in JOINT DATA BASE CONNECTIVITY. this is where you use private constructors. This is just a simple situation there are many situations in which you can use private constructors.
 
vish vodeyar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I hope you know the concept of inheritance.When you inhertit a class all the public methods can be accessed and they can be over ridden or they can be overloaded depending upon the circumstances. If you have a private constructor you cant get that one in the derived class and you cant instantiate it.
To make this possible many of the people use private constructors as i stated you in JOINT DATA BASE CONNECTIVITY.
I hope this satisfies your doubt. I have tried to explain you to the best of my knowledge i have.
 
Ranch Hand
Posts: 111
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vodeyar,

Can you explain me further what is exactly

DATABASE CONNECTIVITY

and what benefit private constructor will give in this scenario.

Regards,

Abdul Mohsin
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought another use for private constructors was for code redundancy.

Like,



Hope this makes since. I�m in a hurry, got a job interview to go to.

Richard
 
Apna Apana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Sorry for you all for late reply
Thanks a lot.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vodeyar vodeyar:
hi
I hope you know the concept of inheritance.When you inhertit a class all the public methods can be accessed and they can be over ridden or they can be overloaded depending upon the circumstances. If you have a private constructor you cant get that one in the derived class and you cant instantiate it.



I think constructors aren't methods and they can't be inherited or overridden no matter what access modifiers they have.
 
reply
    Bookmark Topic Watch Topic
  • New Topic