• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is that possible?/
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • 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.
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • 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?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic