+ 2
Can someone tell me what's wrong with this code it's showing error for getters and setters
I have been trying to master getters and setters in java for a while now so please guys help me out so that i can understand it better please add some explanation and suggestions to understand getters and setters https://code.sololearn.com/cC6NZha5d41F/?ref=app thanks
5 Answers
+ 3
1) You left out the return value for your getter.
2) You had static member functions in a non-static class.
The below works:
class phone
{
String color;
String model;
phone(){
this.color="RED";
}
phone(String c){
this.color=c;
}
public String getColor(){
return color;
}
public void setColor(String c){
this.color=c;
}
}
+ 2
yeah it works
but what was the problem using static there and not using String method declaration in getter
0
but whats creating an error @xan
0
No error now.
0
A getter GETS a value. It's the return that makes that happen. The compiler will cause an error if a getter has no return.
Getters and setters can only be static if they get and set static fields.