65 879
Assignments Done
99,1%
Successfully Done
In October 2018

Answer to Question #20041 in Java | JSP | JSF for nohra

Question #20041
9.Where should the following statement be located in the body of a subclass constructor?
super( );
Expert's answer
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!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions