Default Constructor

I am aware that Java supplies a default constructor if one is not specified but to I need to explicitly make this default constructor private so that a client cannot instantiate it using the default constructor. I want to force them to use the constructor I have specified e.g.

publicclass myClass(){

// I only want a client to be able to instantiate the myClass

//object using this constructor

public myClass(myParameter){

}

// Do I need this?

private myClass (){

}

}

[919 byte] By [java_swing_dudea] at [2007-11-26 18:22:16]
# 1

Test it out.

public class Test

{

int i;

public Test(int a)

{

i = a;

}

public static void main(String[] args)

{

Test t = new Test();

}

}

CaptainMorgan08a at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 2

>// Do I need this?

No, not unless you call it yourself in this class.

When you declare another constructor, the compiler doesn't try to generate a no-argument constructor. This is easy to test and see:

public class X {

public X(int y){}

static void f() {

new X(); //syntax error

}

}

DrLaszloJamfa at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 3
The compiler only adds a default constructor if you supply none. Since you are supplying a constructor, the compiler won't add one, so the only way to create an object will be with your constructor.
hunter9000a at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 4
D'oh! Now I'm the slow one.
DrLaszloJamfa at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 5
You don't need the private one, in Java a no-agrs default constructor is only required if you do not provide one. Your code will work as you require with the top constructor that takes one arg.
kikemellya at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 6
> D'oh! Now I'm the slow one.You still beat some of us :)
hunter9000a at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 7
> D'oh! Now I'm the slow one.at least you didn't come last :(
kikemellya at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 8
> D'oh! Now I'm the slow one.You still beat two others. ;-)...Now I'm the slow one...
CaptainMorgan08a at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 9
Anyone else? ;-)
DrLaszloJamfa at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 10

Well, since everybody else is doing it...

Constructor rules:

1) Every class has at least one ctor.

1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().

1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...}

if you want one.

1.3) Constructors are not inherited.

2) The first statement in the body of any ctor is either a call to a superclass ctor super(...)

or a call to another ctor of this class this(...)

2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super()

as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.

2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.

jverda at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 11
> Anyone else? ;-)We still haven't heard from JosAH, he's due any second now.
hunter9000a at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 12
i hv doubt abt contractor to use 4 constructin
kablaira at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 13
> > Anyone else? ;-)> > We still haven't heard from JosAH, he's due any> second now.jverd did, though.
CaptainMorgan08a at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 14
> jverd did, though.Am I the only one who thinks that parts of the JLS sound like descriptions of the Holy Hand Grenade of Antioch as read from the Book of Armaments?
DrLaszloJamfa at 2007-7-9 5:56:10 > top of Java-index,Java Essentials,Java Programming...
# 15

> > jverd did, though.

>

> Am I the only one who thinks that parts of the JLS

> sound like descriptions of the Holy Hand Grenade

> of Antioch as read from the Book of

> Armaments?

That's not the JLS. That's me. I'll take it as a major compliment! :-)

jverda at 2007-7-9 5:56:11 > top of Java-index,Java Essentials,Java Programming...
# 16

First shalt thou take out the Holy Pin. Then, shalt thou count to three. No more.

No less. Three shalt be the number thou shalt count, and the number of the

counting shall be three. Four shalt thou not count, nor either count thou two,

excepting that thou then proceed to three. Five is right out. Once the number

three, being the third number, be reached, then, lobbest thou thy Holy Hand

Grenade of Antioch towards thy foe, who, being naughty in My sight, shall snuff it.

floundera at 2007-7-9 5:56:11 > top of Java-index,Java Essentials,Java Programming...