Can someone tell me what's wrong with this code it's showing error for getters and setters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

10th Apr 2018, 8:40 AM
Devansh Gupta
Devansh Gupta - avatar
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; } }
10th Apr 2018, 8:58 AM
Emma
+ 2
yeah it works but what was the problem using static there and not using String method declaration in getter
10th Apr 2018, 9:03 AM
Devansh Gupta
Devansh Gupta - avatar
0
but whats creating an error @xan
10th Apr 2018, 9:00 AM
Devansh Gupta
Devansh Gupta - avatar
0
No error now.
10th Apr 2018, 9:00 AM
Emma
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.
10th Apr 2018, 9:07 AM
Emma