Questions tagged [getters]
The getters tag has no summary.
25 questions
4 votes
2 answers
2k views
Should I expose an instance's state with methods?
I've read somewhere about a pattern that is as follows: if it'll change the state of the instance, the method should be named with a verb and return void; and if the method just returns something (...
2 votes
1 answer
451 views
Difference between `Class.X` and `Class.getX()`?
Might be a silly question or something I might have just messed up in my head but here we go... I saw a code example of someone using getPos() in their own class to retrieve the current position of an ...
-2 votes
2 answers
767 views
Why use private access modifier if we need to access private variables anyway?
Everywhere is said that a private member can only be accessed from inside the same class, but at the same time, we can access the private member using getters and setters, therefore my question is: ...
84 votes
12 answers
31k views
What is the utility and advantage of getters & setters especially when they are merely used to read and assign values to properties of an object? [closed]
I’m still really new to learning to program. Just learning the syntax for a few programming languages at the moment. The courses I viewed for C# and Java touched only very briefly on getters & ...
2 votes
2 answers
428 views
The usage of getter notation inside the context of the class
Consider the following JavaScript code: class MyClass { #myPrivateField; constructor() { #myPrivateField = new AnotherClass(); this.theGetter.method1(); // or: this.#myPrivateField....
3 votes
4 answers
2k views
Should a class provide public mutators for all its private fields?
I work on refactoring an Java application based on a CAST audit. One of the criterion says that To respect OO encapsulation concepts, private fields should always be accessed through accessors So ...
0 votes
1 answer
595 views
use always get and set methods is a bad practique, is call directly an attribute class a bad practique in OOP?
I have a doubt about if there are some recommendations for call directly an attribute in a class I think that in OOP you always should call an attribute by the get method. For example: On set ...
0 votes
0 answers
2k views
Are groovy automatic getters and setter effectively any different to public variables?
To provide a very blunt example (as I am at work and can't currently think of a sensible example). If I write a groovy class like this class Wendy{ byte[] frank String doSomethingWithFrank(){...