• 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 constructor

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code is working and gives the output:

ddd0
0 its a method null

so where is the problem??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. Thats right.


SubAlpha is never called.
 
harmeet saini
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frits
i think i just overlooked that code statement
 
reply
    Bookmark Topic Watch Topic
  • New Topic