Reputation: 2467
So 2 questions:
Upvotes: 0
Views: 166
Reputation: 78
It's generally a good idea to make all fields private, and to implement getters and setters as needed. That way, you can make sure that the fields aren't used/changed in a way that is unintended. For example, you can add error checking code to your setters, to make sure fields aren't set to values that will break other things. If other classes can set the field directly, you can't prevent that.
If you have a good reason to make a field with package visibility, or public, then of course you can do so, but you have to be aware of what problems that can cause if other people/classes use your fields the wrong way.
It probably doesn't make a lot of difference in your small project, because you know not to set fields to the wrong values, or screw up otherwise - but if other people use your classes or your project grows in size, you will appreciate the fact that the actual fields are private and you can control access via the getters/setters.
Upvotes: 1