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
29
votes
11 answers

Java :Setter Getter and constructor

I'm a bit confused about the use of getter/setters and constructors (see the below code for an example) public class ExampleClass { private int value = 0; public ExampleClass () { value = 0; } …
user2088260
28
votes
3 answers

Monitor All JavaScript Object Properties (magic getters and setters)

How do I emulate PHP-style __get() and __set() magic getter/setters in JavaScript? A lot of people say that this is currently impossible. I am almost certain that it is possible because projects like nowjs (http://nowjs.com) do something like…
BMiner
  • 16,669
  • 12
  • 53
  • 53
28
votes
5 answers

C++ const in getter

I'm still learning about C++ and I'm reading everywhere that I have to use const everywhere I can (for speed reason I think). I'm usually write my getter method like this: const bool isReady() { return ready; } But I've seen that some IDE…
nkint
  • 11,513
  • 31
  • 103
  • 174
27
votes
8 answers

Is there a way to automatically generate getters and setters if they aren't present in C++?

I'm experienced with Objective-C, and in Objective-C you can let the compiler generate getters and setters for you if they aren't already present (@synthesize). Is there a way to do this in C++, or do I need to implement all getters and setters…
user142019
27
votes
11 answers

How to trace a NullPointerException in a chain of getters

If I get a NullPointerException in a call like this: someObject.getSomething().getSomethingElse(). getAnotherThing().getYetAnotherObject().getValue(); I get a rather useless exception text like: Exception in thread "main"…
Lena Schimmel
  • 7,203
  • 5
  • 43
  • 58
26
votes
2 answers

Cannot use mutating getter on immutable value: 'self' is immutable error

I'm trying to reuse an older piece of Swift code, but getting an error 'Cannot use mutating getter on immutable value: 'self' is immutable error'. Xcode wanted to add 'mutating' before the func, and offered to do so through a 'fix'. So the error is…
arakweker
  • 1,535
  • 4
  • 18
  • 40
26
votes
4 answers

Get Getter Function in Javascript

In JavaScript there is the possibility to create getters and setters the following way: function MyClass(){ var MyField; this.__defineGetter__("MyField",function(){ return MyField; }); this.__defineSetter__("MyField",function(value){ MyField…
Van Coding
  • 24,244
  • 24
  • 88
  • 132
25
votes
14 answers

Is object creation in getters bad practice?

Let's have an object created in a getter like this : public class Class1 { public string Id { get; set; } public string Oz { get; set; } public string Poznamka { get; set; } public Object object { …
user137348
  • 10,166
  • 18
  • 69
  • 89
24
votes
3 answers

Java Interop: Apply @JvmName to getters of properties in interface or abstract class

Usually, we can write the following code in kotlin: val hasValue : Boolean @JvmName("hasValue") get() = true This will generate the method hasValue() instead of getHasValue() for Java interop. However, in an interface, this gives me a compile…
msrd0
  • 7,816
  • 9
  • 47
  • 82
24
votes
2 answers

ES6: How to access a static getter from an instance

How can i access a static getter from an instance of the class that implements that getter? for example, i have this class: class Component { static get isComponent() { return true; } constructor() {} } const c = new Component(); how can i…
Giovanni Bruno
  • 844
  • 2
  • 9
  • 13
24
votes
8 answers

TDD, DDD and Encapsulation

After several years of following the bad practice handed down from 'architects' at my place of work and thinking that there must be a better way, I've recently been reading up around TDD and DDD and I think the principles and practices would be a…
Justin
  • 564
  • 4
  • 13
23
votes
7 answers

Getters/setters in Java

I'm new to Java, but have some OOP experience with ActionScript 3, so I'm trying to migrate relying on stuff I know. In ActionScript 3 you can create getters and setters using the get and set keywords, meaning you create a method in the class and…
George Profenza
  • 50,687
  • 19
  • 144
  • 218
23
votes
3 answers

Swift 4.2 Setter Getter, All paths through this function will call itself

With swift 4.2 I have begun to see a lot of issues, and one of them i'm not really sure how to resolve, since my getter method should be returning the value itself. I imagine what is happening is that the getter will attempt to access the getter…
Travis Delly
  • 1,194
  • 2
  • 11
  • 20
21
votes
5 answers

Reasons for defining non-const 'get' member functions?

I'm working on learning C++ with Stroustrup's (Programming Principles & Practice Using C++) book. In an exercise we define a simple struct: template struct S { explicit S(T v):val{v} { }; T& get(); const T& get() const; void…
Juri
  • 604
  • 9
  • 19
21
votes
6 answers

Interface with getter and setter in c#

As I read here http://msdn.microsoft.com/en-us/library/75e8y5dd%28v=VS.100%29.aspx It is possible to have get in an Interface BUT NOT set ? OR if I want getter and setter in Interface, do I have to use the old syntax getVar setVar just because new…
user310291
  • 36,946
  • 82
  • 271
  • 487