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()); } }

19th Oct 2016, 3:30 PM
Giridhar Gowri
Giridhar Gowri - avatar
6 odpowiedzi
+ 3
convention
19th Oct 2016, 3:42 PM
Wanderlei Borges
Wanderlei Borges - avatar
+ 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.
19th Oct 2016, 5:07 PM
Ousmane Diaw
+ 1
Its a tradition, you can call your method any name you want
19th Oct 2016, 4:16 PM
D Team
D Team - avatar
+ 1
@ousmane thanks!! It's makes sense.
19th Oct 2016, 5:09 PM
Giridhar Gowri
Giridhar Gowri - avatar
+ 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
20th Oct 2016, 3:42 AM
seraphimx
seraphimx - avatar
0
Yea..get and set are more meaningful. But still its not compulsory right..? Thanks!!
19th Oct 2016, 4:19 PM
Giridhar Gowri
Giridhar Gowri - avatar