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
91
votes
9 answers

JavaScript getter for all properties

Long story short: I'm in a situation where I'd like a PHP-style getter, but in JavaScript. My JavaScript is running in Firefox only, so Mozilla specific JS is OK by me. The only way I can find to make a JS getter requires specifying its name, but…
arantius
  • 1,715
  • 1
  • 17
  • 28
90
votes
6 answers

How to define setter/getter on prototype

EDIT Oct 2016: Please note this question was asked in 2012. Every month or so someone adds a new answer or comment that refutes an answer, but doesn't really make sense to do so as the question is probably out of date (remember, it was for Gnome…
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
86
votes
3 answers

Difference between @interface definition in .h and .m file

Normally we use @interface interface_name : parent_class { ...... } @end method in .h file and in .m file we synthesis the properties of variables declared in .h file. But in some code, this @interface.....@end method is kept in the .m…
Rajkanth
86
votes
15 answers

Public Data members vs Getters, Setters

I am currently working in Qt and so C++. I am having classes that has private data members and public member functions. I have public getters and setters for the data members available in the class. Now my question is, if we have getters and…
liaK
  • 11,422
  • 11
  • 48
  • 73
85
votes
10 answers

Conventions for accessor methods (getters and setters) in C++

Several questions about accessor methods in C++ have been asked on SO, but none was able satisfy my curiosity on the issue. I try to avoid accessors whenever possible, because, like Stroustrup and other famous programmers, I consider a class with…
Noarth
  • 3,981
  • 6
  • 23
  • 16
85
votes
12 answers

What is the point of getters and setters?

Possible Duplicate: Why use getters and setters? I have read books on Java, saying that it is good to create setters and getters for variables such as x and y. For example: public int getX(){ return x; } public void setX(int x){ this.x…
Anonymous181
  • 1,863
  • 6
  • 24
  • 27
72
votes
6 answers

Lombok getter/setter vs Java 14 record

I love project Lombok but in these days I'm reading and trying some of the new features of java 14. Inside the new capability, there is the record keyword that allows creating a class with already built-in the following functionality: constructor,…
gixlg
  • 1,193
  • 1
  • 9
  • 21
71
votes
13 answers

Looking for a short & simple example of getters/setters in C#

I am having trouble understanding the concept of getters and setters in the C# language. In languages like Objective-C, they seem an integral part of the system, but not so much in C# (as far as I can tell). I have read books and articles already,…
CM90
  • 825
  • 1
  • 7
  • 9
67
votes
6 answers

Magic __get getter for static properties in PHP

public static function __get($value) does not work, and even if it did, it so happens that I already need the magic __get getter for instance properties in the same class. This probably is a yes or no question, so, it is possible?
treznik
  • 7,955
  • 13
  • 47
  • 59
61
votes
4 answers

Abstract property with public getter, define private setter in concrete class possible?

I'm trying to create an abstract class that defines a property with a getter. I want to leave it up to derived classes to decide if they want to implement a setter for the property or not. Is this possible? What I have so far: public abstract class…
comecme
  • 6,086
  • 10
  • 39
  • 67
59
votes
5 answers

What should a C++ getter return

What is the best practice for a C++ getter method which is supposed to return a non trivial type, but a member which is of type class, or struct. Return by value, such as: MyType MyClass::getMyType() { return mMyType; } Return by const reference: …
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
56
votes
5 answers

Post Java-14 getter/setter naming convention

Java 14 introduced records feature. Record creates getter with the same name as field, so one would write print(person.name()) for example. But old Java bean convention dictates that one should name this method as getName(). Using both styles in the…
vbezhenar
  • 11,148
  • 9
  • 49
  • 63
53
votes
6 answers

Rhino Mocks AssertWasCalled (multiple times) on property getter using AAA

I have a mocked object that is passed as a constructor argument to another object. How can I test that a mocked object's property has been called? This is code I am using currently: INewContactAttributes newContact =…
Confused
  • 869
  • 1
  • 7
  • 16
52
votes
13 answers

Allen Holub wrote "You should never use get/set functions", is he correct?

Allen Holub wrote the following, You can't have a program without some coupling. Nonetheless, you can minimize coupling considerably by slavishly following OO (object-oriented) precepts (the most important is that the implementation of an object…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
49
votes
16 answers

Set and Get Methods in java?

How can I use the set and get methods, and why should I use them? Are they really helpful? And also can you give me examples of set and get methods?
user759630
  • 679
  • 2
  • 8
  • 11