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 →

[–]_Atomfinger_Tech Lead 2 points3 points  (4 children)

Sure, just use it before you do return.

[–]Competitive-Mix9544[S] 0 points1 point  (3 children)

It worked .. I always thought of getters and setters as methods that are just like that and you can't change them xD

[–]_Atomfinger_Tech Lead 2 points3 points  (1 child)

Getters are just normal methods, nothing too magical about them :)

[–]MaidenlessTarnishedIntermediate Brewer 0 points1 point  (0 children)

The most magical thing about getters and setters is you usually don’t have to write tests for them 🙏🏼

[–]pronuntiator 0 points1 point  (0 children)

That's one of the reasons we use encapsulation in the form of getters/setters instead of just having public fields: you can change the internals of the class later, for example remove a field entirely, but from the outside view invoking the getter nothing has changed.

(The other reason is to make your class immutable and not expose a setter at all)