Knute Snortum wrote:I've never heard that getters are evil, but if you want to create an immutable object, then you don't want to use setters. You want to pass the arguments to the constructor so that the fields can be final.
That said, you can still use setters if there is a good reason.
Knute Snortum wrote:Well, say you want a Card class for a game. You could write:
AhFai Chan wrote:Do you have practical examples of immutables in real live, other than math stuff like PI, natural log etc ( which I can get from Math class)
Those values are all doubles which are primitives. The concept of mutability does not apply to primitives. You can either reassign them or you can't (if marked final). The thing about primitives is that you can have hundreds of variables all pointing to the same value of a primitive but, unlike a reference type, they are all independent from one another. You can reassign one of them without affecting any of the others.
AhFai Chan wrote:. . . Do you have practical examples of immutables in real live, other than math stuff like PI, natural log etc . . .
Fred Kleinschmidt wrote:...the setter then sets the value and calculates the new area - once -so the user can call getArea() over and over without triggering another calculation.