This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private constructors" Watch "private constructors" New topic

private constructors

bindu ramesh

Joined: Mar 15, 2001
Posts: 3
Hello Friends
I had some problem regarding the private constructor and having the final class. according to Robert Heller the Math class is final (so that it cannot be subclassed) and the constructor is private(so that it cannot have instance).
Now the Problem is that i was trying to subclass a class whose constructor is private. it was not working as showing error that constructor is declared private.
Programme is as follow: file name

class A
private A(){}
class B extends A
Mafalda Alabort

Joined: Mar 02, 2001
Posts: 24
A constructor will call the no arguments constructor in the superclass, so you'll get the compile error because dont have access to it.
You didn't define any constructors in B, but the compiler will still generate one.
This code will compile as you explicitly call another (non private) constructor:
class A{
private A(){}
A(int i){}
public class B extends A{
Hope it helps
[This message has been edited by Mafalda Alabort (edited March 21, 2001).]
lee dalais
Ranch Hand

Joined: Feb 28, 2001
Posts: 67
hi bindu
what's i think is happenning is that when u compile class B, u have not declared a constructor, so the compiler declares a default constructor, like, b(){super();}, it calls super() to initialize the superclass, but u have one private constructor, so the super(); call refers to that constructor, but that contructor is private, so u get an error.
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Hi Bindu,
in my opinion...,to answer this question, first we have to understand why java allowed constructor to be declare private.
The reason why this constructor is private is some programmers might want to control how an object is created and prevent someone from directly accessing a particular constructor, because "private" is accessible only to the class which defines it.
So user can not create this object via it's constructor.
But usualy the programmers define a static method for the user to create an object of that class.
for example :
here I changed you code to make it work...
class A{
private A(){}
public static A makeA() {return new A();}
public class B {
public static void main(String[] args) {
A a = A.makeA();
it works...!,
BUT this works only with COMPOSITION, not inheritance, as you see, i did not use keyword "extends A" in class B, because if you use extends, this code dosen't work.
because one of inheritance side effect is :
"Java automatically inserts calls to the base-class constructor in the derived-class constructor - either using default constructor or using super keyword".
that's why when you extend to the Base class using "class B extends A", it won't work, because the derived class can not call the base class constructor (again, it's private).
Hope this Help
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Sorry, here is the bonus :
according to my post,
that's why math class is FINAL (it can not have sub-class), and why there is a bounce of static method in Math class.
thank you
Rahul Pawar
Ranch Hand

Joined: Mar 13, 2001
Posts: 38
Can we have static contructors or in short what r the possible modifiers for the constructors.
thanks in advance.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
No you can not have static constructors. A constructor's purpose is to create an instance of the class, static means it relates to the WHOLE class not an instance.
A constructor can have any level of access specifier (public, protected, friendly, private).

8.8.3 Constructor Modifiers

ConstructorModifiers ConstructorModifier
ConstructorModifier: one of
public protected private
The access modifiers public, protected, and private are discussed in �6.6. A compile-time error occurs if the same modifier appears more than once in a
constructor declaration, or if a constructor declaration has more than one of the access modifiers public, protected, and private.
Unlike methods, a constructor cannot be abstract, static, final, native, strictfp, or synchronized. A constructor is not inherited, so there is no need to
declare it final and an abstract constructor could never be implemented. A constructor is always invoked with respect to an object, so it makes no sense for a
constructor to be static. There is no practical need for a constructor to be synchronized, because it would lock the object under construction, which is normally
not made available to other threads until all constructors for the object have completed their work. The lack of native constructors is an arbitrary language design
choice that makes it easy for an implementation of the Java virtual machine to verify that superclass constructors are always properly invoked during object creation.
Note that a ConstructorModifier cannot be declared strictfp. This difference in the definitions for ConstructorModifier and MethodModifier (�8.4.3) is an
intentional language design choice; it effectively ensures that a constructor is FP-strict (�15.4) if and only if its class is FP-strict.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Rahul Pawar
Ranch Hand

Joined: Mar 13, 2001
Posts: 38
Thanks cindy ,
thanks for ur kind responce
I agree. Here's the link:
subject: private constructors
Similar Threads
MATH and null bug me.....
About private constructors!
Extending a class with private constructor
Constructor in private
Math Class is Final?