• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

private constructor ?

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question in following site
--------------------------------------------------
http://www.certmag.com/issues/feb02/sg/answer6.cfm
--------------------------------------------------
In regard to constructors, how can you prevent a class from being instantiated?
A. Use the private declaration.
B. Use anonymous classes.
C. Employ overloading.
D. Use only static inner classes.

Answer they have given is
A. Declaring the constructors as private will prevent the class from being instantiated
but this is not true !!!
the class is instanstiated even when the constructor is private !!!
so what is the correct answer ??
thanks,
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example you cannot create an instance of Math class because its constructor is private.
---------------
Sainudheen
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the constructor is declared private, the class can be instanciated only within its methods.
The following fails to compile:
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In regard to constructors, how can you prevent a class from being instantiated?


I think auther meant to say:

In regard to constructors, how can you prevent a class from being instantiated from outside the class ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Declaring the constructors as private will prevent the class from being instantiated"
Technically speaking , the above statement is talking about creating instance from somebody(Subclass,other class in the same package or other package...) not within the instance. So the answer is true , you can not create an instance of a class whose costructors are private.
To add more with Vod's posting ,Take this scenario. For the first time the class is loaded in the memory and there are no instances. How do the first instance getting created? According to Vod , you can use instance methods to create but there are no instances at first. The answer is you can have class methods(static methods) to create the instance when the constructor is private. Since private can be accessed only inside the class , this would work with no problems.
This is the concept of singleton pattern which we use in java a lot. The reason to go for private constructor when designing an application is to protect access from other classes but only its own class can manipulate and maintain the instances.
[ October 01, 2003: Message edited by: Asha Sat ]
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I tried to describe a trivial non-static scenario of instanciation. Of course, to be able to call instance methods of a class with private constructors, an instance of it must first be created from a static context (whether it's main() or a static method called from main(), or a static reference). Then, an instance method can be called on the object reference which in turn can instanciate a class from a non-static context.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Barkat Mardhani. The question is so poorly worded that the given answer is in fact, incorrect.
I'd like to point out to SCJP 1.4 candidates that these type of erroneous questions are not presented in the exam and you needn't worry about the wording of the questions. When I did the exam nearly a year ago (Dec. 2002), I found one syntactical error in the exam, but there was never any ambiguity in any of the questions - it was always clear what the exam question wanted from you. I'd be concentrating more on the knowledge required from the Java Language Specification, and if you are ever unsure in situations with poorly worded questions, you can always Try It And See(TM).
Good luck !
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic