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
12
votes
5 answers

Java use getter in for loop or create a local variable?

I have a for loop which runs 4096 times and it should be as fast as possible. Performance is really important here. Currently I use getter methods inside the loop which just return values or objects from fields which don't change while the loop is…
stonar96
  • 1,359
  • 2
  • 11
  • 39
12
votes
5 answers

Objective-C getter/ setter

I'm trying to work my way through an Objective-C tutorial. In the book there is this example: @interface { int width; int height; XYPoint *origin; } @property int width, height; I thought, "hey there's no getter/setter for the XYPoint object.…
pic-o-matic
  • 241
  • 1
  • 3
  • 12
12
votes
3 answers

How to override a getter-only property with a setter in C#?

Update: This question has been revised to make it clearer. The answers below seem to reflect that this method works well. Hopefully this question can help people who need to add a get or set to an existing property. Ran into a problem today…
Nat
  • 1,085
  • 2
  • 18
  • 35
12
votes
12 answers

Why stick to get-set and not car.speed() and car.speed(55) respectively?

Apart from unambiguous clarity, why should we stick to: car.getSpeed() and car.setSpeed(55) when this could be used as well : car.speed() and car.speed(55) I know that get() and set() are useful to keep any changes to the data member manageable by…
namespaceform
  • 690
  • 1
  • 6
  • 14
12
votes
4 answers

initialize during declaration and create shorthand getter/setter

How do I initialize the member variables during declaration and create the getter/setter shorthand? Is it possible or do I have to use the constructor to assign the value? For example I want to do something similarl to this public class Money { …
roverred
  • 1,841
  • 5
  • 29
  • 46
12
votes
1 answer

boolean properties starting with "is" does not work

I have a project that use JSF 2.1 and PrimeFaces. I tried to use a simple referencing #{myBean.matriz} and I got this error: SEVERE: javax.el.PropertyNotFoundException: ... value="#{myBean.matriz}": Missing Resource in EL…
andolffer.joseph
  • 613
  • 2
  • 10
  • 24
11
votes
4 answers

The C# Shorthand getters and setters

How does the Setters and Getters in C# implement Encapsulation? I am not new when it comes to these setters and getters, I have background with programming, specifically java. in java you use setters and getters like this public class Person { …
user962206
  • 15,637
  • 61
  • 177
  • 270
11
votes
1 answer

Difference between getters ending with const and const&

I have watched a talk (exact timestamp, not explained by him) by Nicolai Josuttis (member of C++ standard committee) and he stated that getters should be written like so: const std::string& getName() const& { return memberStringVar; } Ever…
Croolman
  • 1,103
  • 13
  • 40
11
votes
2 answers

c++ const public field vs. a getter method

I want to add unique ID (within a single session) to each object of a certain class. One solution is to use a factory function which increments some static counter. A simpler solution is to add this counter to the class itself, e.g.: class…
bavaza
  • 10,319
  • 10
  • 64
  • 103
11
votes
3 answers

Wondering whether I should just bail on using properties in python

I have been trying to use properties instead of specific setters and getters in my app. They seem more pythonic and generally make my code more readable. More readable except for one issue: Typos. consider the following simple example (note, my…
Ben
  • 111
  • 2
11
votes
2 answers

Why is VBA.Collection.Count a method

The VBA Collection has 5 members, all of which are methods: Add, Count, Item, _NewEnum and Remove. Of the 5 members, the Count method looks like it could/should be a getter rather than a method. Is this a legacy of OOP/MS design from the early days…
ThunderFrame
  • 9,352
  • 2
  • 29
  • 60
11
votes
3 answers

Lazy getter doesn't work in classes

class a { get b() { delete this.b; return this.b = 1; } } var c = { get b() { delete this.b; return this.b = 1; } } console.log(c.b); // works as expected console.log((new a()).b); // throws…
Achshar
  • 5,153
  • 8
  • 40
  • 70
11
votes
1 answer

Is it a good practice to throw Exception inside setters in Java?

To be more specific, I want to write a code that throws IllegalArgumentException if the given value is negative. Should I include this code inside of setter/constructor or should I check the value when the appropriate method is called? (Eg: start(),…
leventunver
  • 3,269
  • 7
  • 24
  • 39
11
votes
1 answer

When to use JavaFX properties setter and getter, instead of using the property directly

I am familiar with Java, but just starting to learn JavaFX, and specifically learn about JavaFX properties. I understand the basic design pattern as shown in the following example from Oracle: package propertydemo; import…
vewert
  • 113
  • 2
  • 7
11
votes
8 answers

Data verifications in Getter/Setter or elsewhere?

I'm wondering if it's a good idea to make verifications in getters and setters, or elsewhere in the code. This might surprise you be when it comes to optimizations and speeding up the code, I think you should not make verifications in getters and…
TiTi
  • 363
  • 1
  • 7
  • 15