0
Why we have to exactly use get and set key words to write getter and setters methods?
Why we have to exactly use get and set key words to write getter and setters methods? Even normal functions work similar if we use that in same way of getter/ setter. Please check "getset Mod" program in playground!! public class Vehicle { private String color; // Getter public String putColor() { return color; } // Setter public void giveColor(String color) { this.color = color; } } class Program { public static void main(String[ ] args) { Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle(); v1.giveColor("Red"); v2.giveColor("green"); System.out.println(v1.putColor()); } }
6 Answers
+ 3
convention
+ 3
If you're going to be the only person doing the coding then call it what you like since you understand what its doing, however if you're willing to work in an industry then better get using the recommended style since it makes life much easier for other employees to understand whats happening.
+ 1
Its a tradition, you can call your method any name you want
+ 1
@ousmane thanks!! It's makes sense.
+ 1
you should use get and set methods. you make a variable private so it can only be accessed through methods you have specified. That way you will always have error checking to verify user input
0
Yea..get and set are more meaningful. But still its not compulsory right..? Thanks!!