MethodWithoutReturnTypeDRAFT
When declaring a method, it is not necessary to specify the type of value the method will return.
A method declaration does not need to include a return type
CorrectionHere is what's right.
The return type of a method needs to be specified as part of the method’s declaration.
Example Occurrences
Here the type of the field (or whether the field declaration is supposed to include a type) wasn’t really clear. It could be that this lead to the omission of the return type of the getter. However, also the setter method’s return type (which would be void
, independent of the field’s type) is not specified.
public class IntHolder {
private x = integer; // confusion with type in variable declaration
public IntHolder(int variable) {
x = variable;
}
public getX() { // missing return type (int)
return x;
}
public setX(int variable) { // missing return type (void)
x = variable;
}
}
In the following example, the name of the getter and setter methods includes the type of the instance variable, instead of the name. It’s possible that it is the inclusion of the type as part of the method name that lead to the omission of the return type.
public class IntHolder {
private int x;
public IntHolder(int x) {
this.x = x;
}
public getInt() { // missing return type (int)
return this.x;
}
public setInt(int y) { // missing return type (void)
this.x = y;
}
}