• 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

Private Constructors

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from K&B 1.4

Constructors can use any access modifier, including 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 allowsaccess to an instance created from within the class.)


I am trying this concept with an example.
I have created a class

package pkg1;
class privatecons{
int i;
privatecons(int temp){
this.i=temp;
}
public void displaythis(){
System.out.println("the number is : " + i);
}
}

Calling class:

package pkg1;
class test{
public static void main(String args[]){
privatecons myprivatecons = new privatecons(1);
myprivatecons.displaythis();
}
}

when I execute the above class I am getting the following error

test.java:7: privatecons(int) has private access in pkg1.privatecons
privatecons myprivatecons = new privatecons(1);
^
1 error

My question: Can anybody pl tell me when I have the private constructor and when I need to create an object of that calss from different class what should I do ?

Thanks very much
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What their suggestion means is that if you have a private constructor and want to have an instance of the class from outside the class, then you need to provide a static method in the class that instantiates an object of the class, and then returns a reference to that object.

Creating one constructor that is private in a class definition is one recommended way of preventing creating an object of the class from outside.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I try the example it compiles and runs fine. Are you sure the constructor is private?

Typically, you would have a public method getInstance() in the class with the private constructor to create an instance of that class. This is used in the Singleton Design Pattern. But that won't be on the exam.

Also, Bert would have you know there's a whole chapter on the Singleton Pattern in the book Head First Design Patterns.
[ April 19, 2006: Message edited by: J Sato ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic