File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Private constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private constructor" Watch "Private constructor" New topic
Author

Private constructor

Mihai Lita
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Hello,
I`m reading from the K&B SCJP Exam book, and i've stumble upon the next rule for constructors:
- if the constructor has private modifier , if we want to allow an instance of the class to be used, we must provide a static method or variable that allows access to an instance created from within the class.
Why does it have to be a static method?
Can anyone help me with a link or provide a small example.
Thank you,
Mihai Lita
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, it doesn't have to be a static method. But what if it isn't? Then you need an instance of the class so you can call that method on the instance.

So, now to create an instance of the class you have to have another instance of the class first. Where is that other instance going to come from?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3064
    
  33

Its has to be static because- Static fields dont require an instance of the class and with private constructor once cannot create an instance of the class. Search for Singleton in Java- You will get a lot of queries in this forum or on the search as well.


Mohamed Sanaulla | My Blog
kumarjit banerjee
Ranch Hand

Joined: Mar 27, 2011
Posts: 32

Mihai Lita wrote:Hello,
I`m reading from the K&B SCJP Exam book, and i've stumble upon the next rule for constructors:
- if the constructor has private modifier , if we want to allow an instance of the class to be used, we must provide a static method or variable that allows access to an instance created from within the class.
Why does it have to be a static method?
Can anyone help me with a link or provide a small example.
Thank you,
Mihai Lita



With respect to the qoute "we must provide a static method or variable that allows access to an instance created from within the class" it is right wrt the SCJP Exam book, but java also allows to access the private constructor the other way round. Consider the following code. It is self explanatory.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

True however using reflection has many disadvantages and is usually considered a bad practise (of course there are exceptions). It is certainly not something you should use in this situation and reflection is not a part of SCJP. The things Paul said were probably to make the poster think about the problem.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private constructor
 
Similar Threads
constructor with private access modifier
Instantiating a class with private constructor
Invoking Private Constructor
constructors
Is it possible for a class to have a private constuctor???