Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private constructor

 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code is working and gives the output:

ddd0
0 its a method null

so where is the problem??
 
Henry Wong
author
Marshal
Pie
Posts: 21409
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
mohammad shaid
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2418
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah. Thats right.


SubAlpha is never called.
 
harmeet saini
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frits
i think i just overlooked that code statement
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic