0
Y would i want to use getters and setters when i can assign the value of a property from inside the main class?
when I can do this : public class Fruit{ String color; } public class Program{ public static void main(){ Fruit Apple = new Fruit(); Apple.color = "red"; } }
1 Réponse
+ 1
From the same class it doesn't really matter, but if you want to set or get the value of member variable from another class but still want it to remain private (code control), you might want to use getters and setters.
Go through data encapsulation for more details.