I have a syntax error and I don’t Know how to fix it. The code appears correct to me, but Eclipse is telling me that “Constructor call must be the first statement in a
constructor” at the methods setName() and setAge()
public class KeywordThis {
private String name;
private int age;
public KeywordThis(){
this.name = "NULL";
this.age = 0;
}
public KeywordThis(String s, int a){
this.name = s;
this.age = a;
}
public KeywordThis(String s){
this.name = s;
}
public KeywordThis(int a){
this.age = a;
}
public int setAge(int a){
this(a);
}
public String setName(String s){
this(s);
}
public static void main(String args[] ){
}
}
You cannot call a constructor like that from an instance method. You want your setter to change the value of the object you already have, not create a new one. I think you mean to do this:
Also note that your setters don’t usually return values, so I’ve changed them to return type void.