whats the difference if i declare a variable public or not? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

whats the difference if i declare a variable public or not?

20th Oct 2016, 3:46 PM
Baran
Baran - avatar
1 Answer
+ 3
If you set an attribute to public, it can be accessed from outside the class. It is usually a good practice to set all attributes to private, and provide public getters and setters (ie methods to get/set an attribute respectively) as needed.
20th Oct 2016, 5:25 PM
Zen
Zen - avatar