I have a setter method.
Then when another (say generate) method is run, I need to check the value of my fields.
So in the case of String property, I need to know if it contains the value or if it was not set.
So it may be null, "" or something meaningful, there are 3 possibilities.
And it is rather boring to check first for a null value :
if (s != null)
then for an empty String
if (!s.isEmpty())
is there a one-step check here? You can tell me that I can initialize my String field with an empty String. [ IS IT COMMON? ] But what if someone passes a null value to the setter method setS?
so do we always have to check if the Object value is null or not before doing something with that object?
Well, yes a setter method can check it's values and also a getter method can return a non-null value if the field is null. But is it the only solution? It 's too much work in getters & setters for a programmer to do!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…