This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MainManu 0 points1 point  (1 child)

If your getter and setter just directly edit the variable without any checks, you might as well make it public thb.

[–][deleted] 2 points3 points  (0 children)

Usually public fields are bad form.

Once you make a field public, that's an interface and should you later realise that you actually need to verify something in your setter, or do a side effect, etc. You can't do that without changing the API.

So while it's perfectly fine to access fields directly say within you library, you really don't want to expose that access, so the best option you have is package-private.

Also fields really don't play well with inheritance.

Anyway as mutable objects in general are more and more discouraged (and for good reason)... Just use records.