9.Where should the following statement be located in the body of a subclass constructor?
super( );
Constructor call must be the first statement in aconstructor
if you try to set up some variables and then call aconstructor. You need to construct the object first, then modify it.
WRONG
publicclass Foo {
int i;
public Foo() {
i = 2;
}
public Foo(int x) { // fine
this();
this.i = x;
}
public Foo(int x, int y) {
int z = x*y;
this(z); // not okay
}
}
RIGHT
publicclass Foo {
int i;
public Foo() {
i = 2;
}
public Foo(int x) {
this();
this.i = x;
}
public Foo(int x, int y) {
this(x*y); // now okay
}
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
JavaJSPJSF