File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private constructor" Watch "private constructor" New topic
Author

private constructor

mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
From K&B 5
page 129

Rules For Constructors :
If the private Constructors class wants to allow an instance of class to be used,the class must provide a static method or variable that allows access to an instance created from within the class


when i try to make a private constructor of a class and run normally,it works well.. what i mean to say here is i dint understand this particular statement;;

can someone elaborate please..


Thanks & Regards,
shaad
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
If your constructor is private then 'new' will not work (try it). It follows that without an
object, any instance methods cannot be invoked. For example, aa.getName() cannot
work if aa is null. What's the solution - a static method. It works fine without 'new'
because no object is required. Here is a common way to create a MyClass object.
The private constructor is called from within the getInstance() method and the object
created is returned.Jim ... ...


BEE MBA PMP SCJP-6
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86


This code is working and gives the output:

ddd0
0 its a method null

so where is the problem??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

You do know that your main method is part of the class right? And has full access to the class, including the private constructors.

Try to do it from a main() method from a different class.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

If the private Constructors class wants to allow an instance of class to be used ,the class must provide a static method


i changed this bit : If the private Constructors class wants to allow an instance of class to be used in another class ,the class must provide a static method

variable that allows access to an instance created from within the class


this is what you did

hth

<edit>Oops! Henry beaten me ! </edit>
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

did you mean this?
I tried some thing like this
is this correct?
the program is correct and is running
but is the logic and concept correct?


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
mohammad shaid
Ranch Hand

Joined: May 05, 2010
Posts: 86
Thats what i wanted to clear.. its from Another class.. its not mentioned here in K&B 5 clearly about this.. but i got the concept now...

And prasad thats the code i believe
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Mohammad : I see that you have been quite active since you joined JavaRanch in May.
It's good to see that you are a regular visitor.

Jim ... ...
harmeet saini
Ranch Hand

Joined: Apr 11, 2010
Posts: 33
Jim Hoglund wrote:If your constructor is private then 'new' will not work (try it). It follows that without an
object, any instance methods cannot be invoked. For example, aa.getName() cannot
work if aa is null. What's the solution - a static method. It works fine without 'new'
because no object is required. Here is a common way to create a MyClass object.
The private constructor is called from within the getInstance() method and the object
created is returned.Jim ... ...


I have another doubt related to this topic

following is a question from self test answers from K&B

What is the result?



Answer:- alpha subsub
Reason :-SubSubAlpha extends Alpha! Since the code doesn't attempt to make a SubAlpha, the private constructor in SubAlpha is okay

as per the explanation the constuctor "private SubSubAlpha()" is fine bit what i am not able to understand that once this"private SubSubAlpha()" is invoked isn't it that a default call i.e super(); invokes private constructor "private SubAlpha()" so what does the line mean "Since the code doesn't attempt to make a SubAlpha, the private constructor in SubAlpha is okay" ? as per my understanding the answer should be "alpha sub subsub". please correct me if i am wrong

Thanks in advance


Regards,
Harmeet Singh
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1665
    
  25

Hi Harmeet,

what i am not able to understand that once this"private SubSubAlpha()" is invoked isn't it that a default call i.e super(); invokes private constructor "private SubAlpha()"

The call to super() invokes the constructor of the parent of SubSubAlpha and that is Alpha (SubSubAlpha doesn't extend SubAlpha)

Regards,
Frits
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Yeah. Thats right.


SubAlpha is never called.


SCJP 5, SCWCD 5
harmeet saini
Ranch Hand

Joined: Apr 11, 2010
Posts: 33
Thanks Frits
i think i just overlooked that code statement
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: private constructor