Help coderanch get a
new server
by contributing to the fundraiser
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it possible for a class to have a private constuctor???

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is that possible?/
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors can be declared as private.
A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M SRILATHA:
Constructors can be declared as private.
.....so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.



I did not get this.Can you be a little clear,please?

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The classic example stems from the singleton design pattern. You can find tons of literature on that. but...

if you have a static method, that method can be called even if no instance of the class has been created (think of all the Math class functions).

Now, those static functions are part of the class, so those methods CAN call private functions of the class. So, if the constructors are private, you can call the (public) static methods, which can then call the constructors. the static method then returns the reference to the object.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
up to my knowledge

private constructors are used generally in the following cases

1. to call from the public constructors for your customized needs and when you don't want to disclose to out side world

2. to create an object of your own class and when you don't want other classes to create objects of your own class directly like in the sigleton pattern

see the below example from source code of java.lang.Runtime class

public class Runtime {

private static Runtime currentRuntime = new Runtime();

public static Runtime getRuntime() {
return currentRuntime;
}

private Runtime() {}


that's why we can't instantiate Runtime directly means using new operator
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
up to my knowledge

private constructors are used generally in the following cases

1. to call from the public constructors for your customized needs and when you don't want to disclose to out side world

2. to create an object of your own class and when you don't want other classes to create objects of your own class directly like in the sigleton pattern

see the below example from source code of java.lang.Runtime class

public class Runtime {

private static Runtime currentRuntime = new Runtime();

public static Runtime getRuntime() {
return currentRuntime;
}

private Runtime() {}


that's why we can't instantiate Runtime directly means using new operator
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct Santhi.

sometimes, we can restrict that the objects of our class can built by the methods of the same class only(for getting the singleton property or for some other specific needs). In some cases, the public constructor will call the private one, for some manipulations.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic