Can you explain the setter? I know it's supposed to "set", but I don't know what each piece means and why is it in the class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can you explain the setter? I know it's supposed to "set", but I don't know what each piece means and why is it in the class?

12th Mar 2016, 4:37 PM
also trebeck
2 Answers
+ 7
we have this code : public class Vechicle{ private String color; Vechicle(){ this.setColor("Red"); } Vechicle(String c){ this.setColor(c); } public void setColor(String c){ this.color=c; } } As we know every class has its own variables and its own methods.All variables and methods can be 'public' or 'private' and some others. Public means that the variable or the method (that is public) can be accessed from another class. On the other hand private means that other classes cannot access / read it . In our example , the variable of our class (color) is private so we cannot "take" it / "read" it from another class. For that reason we create the "set" method (here setColor) . Our method is public so we can access this method from another class. This method helps us to access the variable color (from the back door) because the method is public.All set and get methods are written inside their class
17th Mar 2016, 11:29 AM
Xristos
Xristos - avatar
0
wow superb answer
26th Jun 2016, 4:53 PM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar