Questions tagged [getter]

A getter is a public accessor method, used in object-oriented programming, which returns the value associated with a private member of a class.

A getter is public method, used in object-oriented programming, which returns the value associated with a private member of a class. A getter often refers to the get portion of a property. The getter method may perform other tasks such as validation, but is usually considered non-mutating.

1882 questions
2309
votes
23 answers

What is the best way to give a C# auto-property an initial value?

How do you give a C# auto-property an initial value? I either use the constructor, or revert to the old syntax. Using the Constructor: class Person { public Person() { Name = "Initial Name"; } public string Name { get;…
bentford
  • 33,038
  • 7
  • 61
  • 57
1844
votes
37 answers

Why use getters and setters/accessors?

What's the advantage of using getters and setters - that only get and set - instead of simply using public fields for those variables? If getters and setters are ever doing more than just the simple get/set, I can figure this one out very quickly,…
Dean J
  • 39,360
  • 16
  • 67
  • 93
613
votes
28 answers

View's getWidth() and getHeight() returns 0

I am creating all of the elements in my android project dynamically. I am trying to get the width and height of a button so that I can rotate that button around. I am just trying to learn how to work with the android language. However, it returns…
ngreenwood6
  • 8,108
  • 11
  • 33
  • 52
270
votes
9 answers

Why JSF calls getters multiple times

Let's say I specify an outputText component like this: If I print a log message when the getter for someProperty is called and load the page, it is trivial to notice that the getter is being…
Sevas
  • 4,215
  • 3
  • 27
  • 26
259
votes
16 answers

How can we generate getters and setters in Visual Studio?

By "generate", I mean auto-generation of the code necessary for a particular selected (set of) variable(s). But any more explicit explication or comment on good practice is welcome.
Paul
  • 2,813
  • 2
  • 19
  • 12
239
votes
16 answers

Are getters and setters poor design? Contradictory advice seen

I'm currently working on a simple game in Java with several different modes. I've extended a main Game class to put the main logic within the other classes. Despite this, the main game class is still pretty hefty. After taking a quick look at my…
Mike B
  • 12,768
  • 20
  • 83
  • 109
233
votes
15 answers

What are the advantages of using getters and setters instead of functions or simply public fields in PHP?

I'm not a PHP developer, so I'm wondering what the advantages and disadvantages are in PHP to using explicit getter/setters, in a pure OOP style, with private fields (the way I like): class MyClass { private $firstField; private…
Mark
  • 67,098
  • 47
  • 117
  • 162
169
votes
9 answers

Good or bad practice? Initializing objects in getter

I have a strange habit it seems... according to my co-worker at least. We've been working on a small project together. The way I wrote the classes is (simplified example): [Serializable()] public class Foo { public Foo() { } private Bar…
John Willemse
  • 6,608
  • 7
  • 31
  • 45
161
votes
3 answers

What is the "get" keyword before a function in a class?

What does get mean in this ES6 class? How do I reference this function? How should I use it? class Polygon { constructor(height, width) { this.height = height; this.width = width; } get area() { return this.calcArea() } …
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
155
votes
3 answers

Difference in C# between different getter styles

I do sometimes see abbreviations in properties for the getter. E.g. those two types: public int Number { get; } = 0 public int Number => 0; Can someone please tell me if there are any differences between those two. How do they behave? Are both of…
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89
154
votes
4 answers

Best way of invoking getter by reflection

I need to get the value of a field with a specific annotation, So with reflection I am able to get this Field Object. The problem is that this field will be always private though I know in advance it will always have a getter method. I know that I…
Javi
  • 19,387
  • 30
  • 102
  • 135
142
votes
14 answers

Getters \ setters for dummies

I've been trying to get my head around getters and setters and its not sinking in. I've read JavaScript Getters and Setters and Defining Getters and Setters and just not getting it. Can someone clearly state: What a getter and setter are meant to…
oksf
140
votes
14 answers

Simple Getter/Setter comments

What convention do you use to comment getters and setters? This is something I've wondered for quite some time, for instance: /** * (1a) what do you put here? * @param salary (1b) what do you put here? */ public void setSalary(float…
ThaDon
  • 7,826
  • 9
  • 52
  • 84
112
votes
6 answers

How do getters and setters work?

I'm from the php world. Could you explain what getters and setters are and could give you some examples?
ajsie
  • 77,632
  • 106
  • 276
  • 381
111
votes
4 answers

What are getters and setters for in ECMAScript 6 classes?

I am confused as to what the point of getters and setters are in ECMAScript 6 classes. What is the purpose? Below is an example I am referring to: class Employee { constructor(name) { this._name = name; } doWork() { …
TruMan1
  • 33,665
  • 59
  • 184
  • 335
1
2 3
99 100