When must a field be private or public? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

When must a field be private or public?

And when the modifier must be default?? I don't know when we use the default modifier.

8th Aug 2016, 9:33 AM
Lordeblader
2 Answers
+ 6
One practise is to make methods public and variables private. one reason for this idea is that if you make the methods public they are accessible but by making variables private it lessens the chance a program can be over written in memory by using exploits. It helps ensure the program does what it was coded to do. Another reason is that by having private variables that variable can only be read in that method. It reduces how much code needs to be read during debugging as no other method can directly access a private variable. Therefore only that function needs to be referred to to determine where a bug has occurred. but there is no enforcement on when to use access modifiers. it's there at a programmers discretion. this is typical of most things about coding. there's usually more than one way to achieve a solution. complexity is reduced with practised knowledge.
8th Aug 2016, 9:39 AM
Mi You
0
Ok, thank you! :)
8th Aug 2016, 9:41 AM
Lordeblader