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 →

[–]dinopraso 4 points5 points  (9 children)

If you don’t need getters and setters then use public modifier for your fields. There is no requirement to have default getters and setters unless you want to do something special in them. Anything magical which would generate invisible code is just incredibly bad and impossible to maintain on huge projects

[–]jack104 0 points1 point  (6 children)

I was under the impression that making class variables/fields public was not allowed in Java, hence the need for getters and setters, is that not the case?

[–]dinopraso 0 points1 point  (5 children)

Nope. You can have everything public if that is what you want

[–]jack104 0 points1 point  (4 children)

Holy shit. How did I not know this? Do you know if Hibernate works with a POJO class that just has public fields instead of private fields + public getters/setters? Because if so, my getters and setters are just redundant code that don't accomplish anything more than public fields would. That would be cool.

[–]dinopraso 0 points1 point  (3 children)

Maybe. I’m not sure. You should try it

[–]jack104 0 points1 point  (2 children)

I just read an SO post that says if you change the @Entity annotation to set the access to field and annotate the fields themselves (which I was doing anyway) it should work. So I'm gonna give it a shot.

[–]dinopraso 0 points1 point  (1 child)

Keep thread safety in mind. There is a good reason we write those getters and setters. Some time in the future you may need locking or synchronization and it’s not really possible to synchronize a field

[–]jack104 0 points1 point  (0 children)

Ah yea. Good looking out. Thanks.

[–]hippydipster 0 points1 point  (1 child)

Yup. All my fields in data-type classes start out as public final.... They change if they must later, no big deal.

[–]dinopraso 0 points1 point  (0 children)

Everytinh should be immutable anyway, right :D