• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

I know high level classes can not be declared private but...

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know high level classes can't be declared private, because I've seen that question come up enough that I am pretty certain that is true. But is it also true that the constructor can be private...as long as there is a method that calls the constructur within the class? And if that is so, does this method have to be static? Because if it isn't how else can you ever call that method without a handle on the class?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wilson
Yes its true constructors can be private. If you wish to call it from the same class then you can instatiate it the regular way because private methods are available to the class members. Though now you would not be able to instantiate an object of that class using the private constructor outside the class.
Here is some code that will explain you the same.

This is so because Test() (Test's constructor) is private to all but Test itself and so is the case with a. Had there been another constructor in a like a(int i) then you can instantiate a by using that constructor.
 
Wilson Mui
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, do you have any examples where such a feature would be useful...and also if I created a static method that could be accessed by a class outside of the class with the private constructor, I would assume that static method should return an object of the class with the private constructor:

would that work? Is that how it works? now why on earth ever do this?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Yes this type of approach would be useful in case you dont want your objects to be instantiated by other classes.
Can you rewrite you code it has many error (typos and other error). If this the code you you wanted then its fine dont rewrite.

Well good doing so would work quite like a constructor but would probably defeat the purpose of having a private constructor. Note that it would work quite like a constructor but not exactly like it.
One more thing doing so would also prohibit someone from extending your class as the constructor would not be availabe.
[ April 23, 2003: Message edited by: Anupam Sinha ]
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is being described here is actually the Singleton pattern and is used in many situations.
The singleton pattern ensures that there is only one instance of a class and provides a global point of access to that instance of the class.
Example: A window manager inside your application, a database connection pool, print spooler, serial communication, etc.
public class PrintSpooler {
private static PrintSpooler spooler;
private PrintSpooler(){}
public static synchronized PrintSpooler getPrintSpooler() {
if (spooler == null)
spooler = new PrintSpooler();
return spooler;
}
}
In the above example you don't need to worry if the instance has been created, the class handles this for you and you can be sure you always uses the same instance.
Thomas De Vos
http://www.javacertificate.com
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic