I hate getters and setters in java. Half of my code is filled with them. They are pointless boilerplate code that says nearly nothing. 99.9% (I made that number up, but it sounds reasonable) percent of the cases they don't have any additional code in them.
And it is really hard to see whether a variable is read-only/read-write/write-only. You have to find the getters/setters to see that.
I have a notation where I put all the modifiers at the end of the class and separate them with a page of whitespace.
I don't like the C# solution - it's not really shorter, only binds the getter and setter together.
I have a proposition: use annotations:
@Getter @Setter private boolean subscribedForEmailNotification; |
And one wants to write extra code, one should be able to use the old notation. The compiler will check for collisions.
I don't see any reason why this cannot become the next syntactic sugar in java where one saves a bunch of useless code-writing.
Hi Misho,
Other way is to encapsulate the getters and setters in aspects, e.g. when Spring Roo creates an entity it separates its getters and setters in an aspect which is woven to the class at runtime.
Cheers,
Nikolay
I would like a solution on the POJO level. Everyone can set up a solution for his/hers own framework (unfortunately it will not be universal), but only SUN (now Oracle) can fix that the right way 🙂
I know what do you mean.
Have you tried that: http://projectlombok.org/features/GetterSetter.html
It seems related to the annotation-based idea you mentioned above.
It's not a general solution but at least it's something ... 🙂
A discussion on the topic: http://stackoverflow.com/questions/1907312/java-getters-and-setters
Ники,
This looks f*ing awesome.
It is interesting, however, how does that work and whether it has some side effects.
No idea, Misho 🙂 Just stumbled on it after a quick search. That's why I added the stackoverflow discussion on the topic - in it the people are speaking about Lombok's pros and cons apart of the whole discussion 🙂
Ever seen the Ruby way?
http://www.java2s.com/Code/Ruby/Class/Accessors.htm
My Ruby experience equals my rocket science experience. Anyway seems to be exactly what I want - getters and setters without any code.