+ 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.
2 Réponses
+ 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.
0
Ok, thank you! :)