Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using getters within class methods

Tags:

getter

If you have a class with some plain get/set properties, is there any reason to use the getters within the class methods, or should you just use the private member variables? I think there could be more of an argument over setters (validation logic?), but I'm wondering just about getters.

For example (in Java) - is there any reason to use option 2?:

public class Something
{
    private int messageId;
    public int getMessageId() { return this.messageId; }
    public void setMessage(int messageId) { this.messageId = messageId; }

    public void doSomething()
    {
        // Option 1:
        doSomethingWithMessageId(messageId);

        // Option 2:
        doSomethingWithMessageId(getMessageId());
    }
}
like image 516
Andy White Avatar asked Mar 10 '09 05:03

Andy White


People also ask

Should you use getters inside class?

Yes, the methods of your class should call the getters and setters. The whole point in writing getters and setters is future proofing. You could make every property a field and directly expose the data to the users of the class.

Should you use the getter method also whenever you use the setter method?

Getters and Setters Are Highly Overused All fields should be kept private, but, setters should only be kept private when it makes sense, which makes that object Immutable. Adding an unnecessary getter reveals an internal structure, which is an opportunity for increased coupling.

How do you use setters and getters in two different classes?

To fix this, you need to pass a reference to the GetterAndSetter instance from class A to B . You can do this e.g. by passing it as a parameter to a method of B , or by creating a new instance of A in B and calling a method that provides an instance of GetterAndSetter .

Should a class always have getters and setters?

Using getters and setters, is always, in my opinion good practice. One thing you should avoid is to have external entities mess with the internal structure of your class at will. Typical example, consider having a dateOfBirth parameter.


2 Answers

Java programmers in general tend to be very consistent about using getter methods. I program multiple languages and I'm not that consistent about it ;)

I'd say as long as you don't make a getter it's ok to use the raw variable - for private variables. When you make a getter, you should be using only that. When I make a getter for a private field, my IDE suggests that it replace raw field accesses for me automatically when I introduce a getter. Switching to using a getter is only a few keystrokes away (and without any chance of introducing errors), so I tend to delay it until I need it.

Of course, if you want to stuff like getter-injection, some types of proxying and subclassing framworks like hibernate, you have to user getters!

like image 114
krosenvold Avatar answered Sep 17 '22 17:09

krosenvold


With getters you wont accidentally modify the variables :) Also, if you use both getters and the "raw" variable, your code can get confused.

Also, if you use inheritance and redefined the getter methods in child classes, getter-using methods will work properly, whereas those using the raw variables would not.

like image 45
Antti Huima Avatar answered Sep 17 '22 17:09

Antti Huima