Questions tagged [getters]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2 votes
1 answer
375 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
4 answers
696 views

How do I avoid tightly coupling one microservice to another microservice's feature that depends on specific views of the first's data?

I've seen this problem in a few different contexts now but I'm not sure what it's called or how to think about it. Suppose I have a service, AccountService, that serves accounts from a database, e.g. ...
-2 votes
2 answers
409 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: ...
2 votes
4 answers
589 views

Is using getter method violating the law of Demeter?

Suppose I have a Attendance class public class Attendance { private PersonInfo personInfo; public PersonInfo getPersonInfo() { return personInfo; } } And I want to check if person is ...
83 votes
12 answers
27k 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 & ...
4 votes
4 answers
2k views

Backing field versus private set C#

I doubted to post this question to the general StackOverflow, but it is suggested to not post opinion-based questions and this might be one. And ofcourse, this is the software engineering department. ...
2 votes
2 answers
294 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....
  • 129
9 votes
4 answers
1k views

Can renaming a method preserve encapsulation?

I was reading this page, about when getters/setters are justified, and the OP gave the following code sample: class Fridge { int cheese; void set_cheese(int _cheese) { cheese = _cheese; } ...
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 ...
1 vote
1 answer
428 views

Should I use accessors or public static fields for global constants?

I have to work on some code that was CAST-audited. The report says that it is bad in Java to use public static and that accessors should be preferred. That is also what I was taught at school. The ...
0 votes
1 answer
321 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 ...
  • 387
4 votes
1 answer
221 views

Is using getters to exchange information between objects acceptable?

Suppose I have the following Character, Potion, and PotionType classes: class Player: def __init__(self, name: str, health: int, mana: int): self._name = name self._attributes: ...
user avatar
0 votes
0 answers
878 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(){...
  • 123
-2 votes
1 answer
377 views

Set result of getter instead of setter [closed]

Is this a legit use of getter Lady lady = new Lady(); lady.getWater() = "hot water"; if we suppose getter returns Class Lady { public String getWater() { this.water; }} ?
  • 129
2 votes
2 answers
298 views

OOP in Java - What can getters be used for?

Usually, getters always return the value of a variable. I learned in my literature that access to fields is controlled by getters and setters. When I had my code rated by programmers, it was suddenly ...
2 votes
2 answers
453 views

Is there any reason to avoid private getters?

Even though I could write something like this (In C#. There are, of course, equivalents in other languages): public int SomeNumber { private get; set; } I have never encountered something like this. ...
12 votes
5 answers
7k views

Is it a bad idea to use getters/setters and/or properties at all? [duplicate]

I am perplexed by comments under this answer: https://softwareengineering.stackexchange.com/a/358851/212639 A user is arguing there against the use of getters/setters and properties. He maintains ...
  • 3,517
3 votes
2 answers
733 views

Dealing with a lot of getters and setters

I've already asked "Dealing with a large interface". I have a further question regarding that situation. It was pointed out to me that I used a lot of getters and setters and so I broke encapsulation....
7 votes
4 answers
555 views

Encapsulation and Displaying Information

This site and SO contain many pages about getters/setters and if they break encapsulation or enforce it. My question is for those developers that agree that getters/setters break encapsulation and ...
user avatar
4 votes
4 answers
1k views

How exactly are getters and setters defined?

Note: Questions with similar title have been asked before, but please read the full text before claiming that this is a duplicate. Since everybody in OOP uses the terms getter and setter, I would ...
  • 6,381
1 vote
2 answers
419 views

Conflict in getter and setter method names in ruby api design

I am inheriting an api decision in an SDK I am writing where I am required to fetch domain objects (entries) from the server like this: blogEntries = client.content_type('blog').entries As you can ...
0 votes
2 answers
3k views

Should the getters and setters of a stl container access the container itself or the elements inside it?

Consider I have a vector and map as class members: class MyClass{ protected: std::vector<int> myVector; std::map<int,std::string> myMap; }; Should the getter and setter access ...
  • 5,725