Most voted "getters-setters" questions
Getters and Setters are methods used to retrieve and assign values (respectively) to variables without having to work directly with them. If the question is not about "Getters and Setters", do not use this tag even if you are using "Getters and Setters" in your project.
Learn more…64 questions
Sort by count of
-
51
votes4
answers3366
viewsAre getters and setters an illusion of encapsulation?
It is common to teach that it is right to leave the class attributes private and create getters and setters for them, all because of the encapsulation. Isn’t that the same thing as making everything…
-
28
votes5
answers1775
viewsWhy is it bad practice to have int attributes?
I saw in the answer to that question /questions/17015/qual-o-uso-de-uma-variável-estática-ou-final-em-java/17136#17136, that: It is bad practice to have int attributes, unless they are "constant" or…
-
24
votes4
answers1788
viewsAre Getters and Setters mandatory or facilitators?
Lately I have read some Java books, but there is a part that makes me confused in this type of methods accessors - gettters and setters. The point is: I am required to write in this type of methods,…
-
21
votes3
answers5317
viewsWhy use get and set in Java?
I learned in college a while back that you should always use getters and setters for access of values in an object and I heard that it is for security reasons. So I ended up always using and seeing…
-
20
votes3
answers14315
viewsPythonic way of defining setters and getters
Searching the internet, I see that it is highly recommended not to use Setter and Getters in Python classes. However, I cannot determine a way to access private values without using them. There are…
-
17
votes2
answers560
viewsWhy shouldn’t I change the "getter" s and "Setter"s?
I recently came across a friend saying that it is not advisable to change getters and Setters. I had made the modification of a Setter of a List. In it I did not receive a list by parameter, but an…
-
16
votes3
answers3152
viewsWhen to use Setters and Getters?
I’m studying OOP and in the encapsulation part I had a question about when to use getters and setters, besides being able to validate the parameter passed, what is the usefulness of using? I could…
-
16
votes2
answers450
viewsGetters and Setters can only "walk" together?
I am studying object orientation and am having some doubts in the encapsulation part. For example: Class Url { private $url; public function setUrl($url) { if (filter_var($url, FILTER_VALIDATE_URL))…
-
14
votes2
answers625
viewsIs it really necessary to use mutator and access (Setter and getter) methods in PHP? What about performance?
I’ve been noticing that most other libraries use the methods setters and getters (hereinafter referred to as mutator and accessor), to change the property of some classes. For example: class User {…
php performance pattern-design encoding-style getters-settersasked 8 years, 9 months ago Wallace Maxters 102,340 -
11
votes2
answers160
viewsEqual sign in Ruby method definition
I have come across the following definitions of methods and I would like to know the difference between the first definition and the second. This first has no equal sign in the definition: def nome…
-
11
votes3
answers731
viewsWhat is the advantage of using getters/setters in Javascript classes?
For example in this code below I use methods getters and setters in class: class Pessoa { constructor(nome) { this.nomePessoa = nome; } get _nomes() { return this.nomePessoa; } set _nomes(valor) {…
-
10
votes3
answers271
viewsHow to avoid the use of setters in such cases?
In object orientation it is recommended to avoid the use of setters. The usual justification for this is that the logic that modifies the state of an object must be encapsulated in the object.…
-
9
votes3
answers407
viewsGenerate setters only in class constructor or generate outside constructor
In Java classes, it is common (At least in college I only see it that way) create private attributes and generate getters and setters for these attributes. But I read that you can do something a…
-
8
votes5
answers993
viewsWhat is the difference between referencing an attribute directly or by get/set
As an example I have a private String atributo, so I can have two ways of referencing the same, this internally in the class: this.atributo And also: this.getAtributo(); It’s correct to say it’s…
-
8
votes1
answer231
viewsHow to use the 9th rule of Object Calisthenics in PHP?
Object Calisthenics making a translation into Portuguese means "programming exercises", and consists of 9 rules created by Jeff Bay in his book The Thoughtworks Anthology. The Object Calisthenics is…
-
8
votes2
answers335
viewsHow to customize the getter in Kotlin?
When we create a variable of type val, in the case of Java, only the getter relative to it. Different when a type variable is created var, in which the getter and setter. See below for an example:…
-
7
votes2
answers329
viewsHow to transform attributes into properties?
In C#, I can avoid using getter and setter, transforming the attributes into properties, as below: public class Pessoa { public int pes_idade { get; set; } public string pes_nome { get; set; } } You…
-
7
votes1
answer492
viewsUnderstanding the pythonic way of dealing with properties
I am on time trying to understand how this issue of python properties works. But the problem is that in all the tutorials net out only find the damn example with only one attribute. I’m looking for…
python python-3.x getters-setters property decorator-patternasked 9 years, 4 months ago Devel Silva 73 -
6
votes3
answers1817
viewsShould I declare Get’s and Set’s at UML?
Every time I create a class UML I have to declare get and set for all classes that come to use them or can leave without, as if it were standard and everyone knew that they must implement them even…
-
6
votes1
answer1230
viewsWhen using __constructor magic method or set and get
My doubt is with constructor relation, for example, I have a class with name, age. the correct is to use __construct to pass values to them or use set e get ?
-
6
votes1
answer4099
viewsDefinition and use of @Property
Good afternoon, I’m starting to study object orientation and I’m not getting a very good understanding of what the function is and when to use @Property. But from what I understood up to the moment…
-
5
votes2
answers2743
viewsHow do I set a private property in Python? And is there a protected property?
I come from PHP. In it, when we want to define a property of a class as private we add the keyword private in his statement. Example: class StackOverflow extends StackExchange { private $language =…
-
5
votes1
answer302
viewsWhat happens in real life in a developer environment if the programmer does not encapsulate an attribute?
The programmer João went there and created a class Cliente and the attribute public double saldo and the method Sacar() public also. What’s wrong with leaving the attribute double saldo, after all…
oop software-engineering encapsulation getters-setters privateasked 7 years, 6 months ago user3671786 55 -
5
votes2
answers4088
viewsGetters and Setters Methods
In my course, I’m learning getters which takes "data" and setters, inserting/modifying. I made my code like this for the pen class: <?php class Caneta { public $modelo; private $ponta; public…
-
4
votes4
answers257
viewsUse of setters in the builder
I wonder if there’s any difference, semantically speaking, between these two builders: public Aluno(String n, float n1, float n2) { this.nome = n; this.setNota1(n1); this.setNota2(n2); } and public…
-
4
votes3
answers138
viewsIn structures (struct) is it necessary to use getters and setters or only in classes (class)?
It is necessary to use setters and getters in structures to maintain good practice or is needed only in classes?
-
4
votes3
answers218
viewsPublic field X property
I have a string defined as "name" initially it is as private. string nome; In case I assign the methods get/set, and to stay in the pattern I change the initial letter to uppercase, getting: string…
-
4
votes2
answers88
viewsA Setter method can receive a getter + value as a parameter?
I created the advisory methods Setter and getter for the balance attribute. It is correct to use these advisors within each other? One being used as parameter for the other. More specifically the…
-
3
votes2
answers432
viewsCreate a set method for different variables
I’m looking to create a Setter for an object Character, where it will modify the attributes String name, int intellect, int strength and int stamina, but what is the best method to do this? public…
-
3
votes2
answers88
viewsI cannot invoke a Setter or getter in Javascript
I was doing some object orientation exercises and in the solution the teacher used set and get. I realized that even if I do it like the teacher, it doesn’t work. I can solve it without get and set,…
-
3
votes2
answers548
viewsHow to create getter and Setter from Arraylist?
I am creating a question and answer game, with the attributes: question, correct answer and the ArrayList option (private ArrayList<String> opcao = new ArrayList<String>();). I need the…
-
2
votes4
answers151
viewsHow to build a class correctly with access methods?
How do I join several variables into a single object? How do I arrange the following code? <?php class Produto { //Atributos var $cpu; var $mb; var $psu; //Getters & Setters function…
-
2
votes2
answers98
viewsEclipse creating getter with prefix is
I created the methods using the Eclipse Getters and Setters command, but when creating the Boolean attribute getter status, it was automatically created as isStatus. It should not be created as…
-
2
votes1
answer48
viewsError in Method Get on a Collection
I need to use XML in a university job and had problems with a method Get that I had to implement in a Collection. After a while I ended up taking a look at some sites to do this and found the…
-
2
votes1
answer700
viewsModel field processing before writing to the database (Python + Django)
I am trying to perform a password encryption using Python 3.7 and Django 2.1. Looking at the documentation of Python, Django and some answers here in Stackoverflow, I arrived at the code as follows,…
-
2
votes1
answer585
viewsError: "illegal target for variable Annotation" using "@Property"
Like pygame does not have a function similar to the library SFML -> View, I am developing a "camera" format to scroll the screen and preserve the positions of objects within the general…
-
2
votes1
answer125
viewsAttribute or private field and getter and Setter in Java object-oriented programming
In object-oriented programming in Java, when I create any attribute or private field, without their respective methods getter and setter has to define the attribute as final or need not? For…
-
2
votes2
answers395
viewsHow to assign a default date on a Datetime type object in C#?
I created a class called Order, and in it I created a field for the purpose of entering a date of delivery of the order. But I created the object on Main(), and at the time I execute it fills in…
-
2
votes1
answer81
viewsCondition does not work in a Setter method
Why does my code give the following error: The operator '>' cannot be applied to type operands decimal and double. Code: class Funcionario { public string nome { get; set; } public string…
-
1
votes1
answer60
viewsI’m not getting/knowing how to use Setter
I’m trying to do some tests in JAVA here and I’m in trouble: The product class, with the initialization and the getters/setters public class Produtos { private String[] nomeItem = {"Item 1","Item…
-
1
votes1
answer487
viewsGet and Set using PROPERTY - Python3
As an apprenticeship of Property, created a small currency converter. See: class converteMoeda(): def __init__(self, dollar=0, real=0): self.dollar = dollar self.real = real def toDollar(self):…
-
1
votes0
answers47
viewsShould I really use getters and setters for all public variables in the classes?
So this subject seems very controversial but at the same time I don’t see a call among developers. Some say that getters and setters are 100% rule to access any variable within a class. That is, if…
-
1
votes2
answers270
viewsUse a constructor with multiple parameters, or create multiple sets?
Amid use a constructor with multiple parameters or give a lot of sets, which of these options is more performative for the compiler, would cost less memory and would be faster?…
-
1
votes0
answers865
viewsRun function within Date in Vue.Js
In a Vue.Js component I use this Script template as in the example. On a getter, I inserted a Return of what is received const GetSegmento = () => (param) => { return (() => { return param…
-
0
votes1
answer1214
viewsWhy use getters and setters in classes?
Because I need to encapsulate the entire class, if, as a programmer, I know perfectly how to use that variable. I only see need for setters and getters that work the variable value, so: void…
-
0
votes0
answers58
viewsShould I create constructor with Setter? PHP
Which best and safest method to create the constructor? Netbeans creates direct by changing the variable, but has how to use Setter. So, which is the best and safest method? For example: Without…
-
0
votes1
answer49
viewsEN (Spinner) Firebase Exception: Found Conflicting getters for name: getAdapter
I am making an application where I choose a number in the spinner and write that number in Firebase. this giving the following error: details about the spinner and how I’m recording in firebase…
firebase android-adapter spinners getters-setters realtime-databaseasked 7 years, 7 months ago Garcez 31 -
0
votes1
answer34
viewsHow do I value a value passed by Set?
I wanted to know how do I add a new value to length and width through setLength and Setwidth, and this value goes through validation ((x > 0 && x < 20)? x : 0): public class Rectangle…
-
0
votes2
answers9527
viewsSet Difference and Get in object-oriented programming
What is the basic difference of set and get in object-oriented programming?
-
0
votes0
answers93
viewsThe best way to use getters and setters
I started to study programming a short time ago and the chosen language was Python. And I’m finding it very difficult to enter the use of getters, setters and properties. In my internet browsing, I…