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 (){
}
}
Test it out.
public class Test
{
int i;
public Test(int a)
{
i = a;
}
public static void main(String[] args)
{
Test t = new Test();
}
}
>// 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
}
}
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.
D'oh! Now I'm the slow one.
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.
> D'oh! Now I'm the slow one.You still beat some of us :)
> D'oh! Now I'm the slow one.at least you didn't come last :(
> D'oh! Now I'm the slow one.You still beat two others. ;-)...Now I'm the slow one...
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.
> Anyone else? ;-)We still haven't heard from JosAH, he's due any second now.
i hv doubt abt contractor to use 4 constructin
> > Anyone else? ;-)> > We still haven't heard from JosAH, he's due any> second now.jverd did, though.
> 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?
> > 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! :-)
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.