Saying private public and protected is useless? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Saying private public and protected is useless?

I see that these words are used just so the code can be read better. I can literally put public in everything and it would all make no difference. it also grants no security benefits at all.

6th Mar 2018, 9:31 AM
Paul
Paul - avatar
1 Answer
+ 5
Those aren't for security, they're for readability and debugging. If every variable was public and something went wrong, every variable is a potential culprit. Unlike if you used access modifiers, you already have some idea of which variables may have caused the bug. Also making every variable public would mean that if you wanted to change a variable in one class, you would also have to check the dependency of another class on that variable, and another class that may also depend on the previous class and on the first mentioned class, and so on. That would take too long, and would decrease productivity. In short, for small scale programs, access modifiers wouldn't really bother. But for large scale programs with many classes and dependencies, it would be a nightmare without these.
6th Mar 2018, 9:58 AM
apex137
apex137 - avatar