Y would i want to use getters and setters when i can assign the value of a property from inside the main class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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"; } }

13th Jun 2017, 4:55 AM
suryapoojary
suryapoojary - avatar
1 Answer
+ 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.
13th Jun 2017, 5:31 AM
Shahar Levy
Shahar Levy - avatar