5 ms·
Couldn't agree more. Use public, protected, private fields as they were meant to be used. I only dip into JavaBean anymore if I need a readable but non-writable
by exabrial 13y ago
Couldn't agree more. Use public, protected, private fields as they were meant to be used. I only dip into JavaBean anymore if I need a readable but non-writable field.
- karlmdavis 13y agoFor me that's almost all fields: I try to make just about everything immutable by default. Makes reasoning about concurrency much simpler.
- lmm 13y agoFor immutable you can just use public final fields. I assume GP must be talking about fields that are mutated but only by the object itself, where outside code needs to be able to read but not write.
- stevoski 13y agoNot quite. eg: public final Date date; date.setTime(1); That's not immutable.
- lmm 13y agoThe bean convention doesn't help you with that one though; if you have private Date date; public Date getDate() {return date;} then people can still do getDate().setTime(1);
- cdmckay 13y agoYeah, but with a getter you can make getDate return a copy of the Date object to enforce immutability.
- cakeface 13y agoYeah, but getDate().setTime(1); works the same way.
- djur 13y agoNot if getDate() returns a copy of the underlying member.
- mateuszf 13y agoYep, the thing is that Date is immutable. I think that this public final immutable rule is ok, but one has to ensure the fields themself are really not modifiable.
- nl 13y agoThe fact that java.util.Date is terrible is so many ways should go without saying.