This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Constructors" Watch "Private Constructors" New topic
Author

Private Constructors

Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Hi,
If a class has private no-arg constructors, those classes cannot be instantiated? True/ False
"TRUE"
If a class has private constructors, those classes can be subclassed, but the constructors defined in subclasses cannot invoke super constructors ?True/False
"True"
If a class has no_arg private constructors, but if it has other constructors which are private, then the subclass can instantiate the class with out any compiler error?
True/false
True
I just want to know is my ideas clear. Also I would like to know what all the things we need know on AWT and events for exam. Is it necessary to learn each every method in every class. What we have to know on I/O and threads?


Arathi<br />Sun Certified Java Programmer
Maitham H
Greenhorn

Joined: Jan 07, 2001
Posts: 29
for the last part of your questions, try to carefully read and understand the objectives of the exam as stated in the sun web site, they give you good idea on what to read and concentrate on.


A Software life cycle can be greater than its Developer's
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
If a class has private no-arg constructors, those classes cannot be instantiated? True/ False
"TRUE"

FALSE
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Arathi,
your first statment is mostly correct, but I would say that "If a class has a private no-arg constructor, that class cannot be instantiated by any classes using the no_arg constructor."
If there are other non-private constructors, those can be used to instantiate the class. here's a simple example:

Hope this helps
Rob
[ January 08, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Hi Rob,

Hope this helps
Rob
[ January 08, 2002: Message edited by: Rob Ross ][/QB]
The above code works fine. But if you subclass a class, then it gives compiler error. Am i right. Because the subclass will invoke super class constructor and since the super class constructor is private it gives compiler error. Are you getting what i am mentioning. In your code, the class is not subclassed. So it won't be problem when you compile p=new Private("String");
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Private Constructors