What is the Java convention for using get methods inside other methods of the same class?

StackOverflow https://stackoverflow.com/questions/14493892

  •  17-01-2022
  •  | 
  •  

Frage

After I have written a get method in a java class, is it better to use the get method in the same class or the variable itself?

For example:

if(a.getWidth()>this.getWidth())

or:

if(a.getWidth()>this.width)

Also I am confused if i should be using the this.anything so much. It seemed easier to read when comparing objects of the same type to each other.

War es hilfreich?

Lösung 3

1) It may seem from inside a class that there is no difference between using field and getter but what if a getter is overridden by a subclass?

class A {
    String name;
    String address;

    String getName() {
        return name;
    }

    String getAddress() {
        return address;
    }

    String getDescription() {
        return name + " " + address;
    }
}

class B extends A {
    String country;
    @Override
    String getAddress() {
        return super.getAddress() + ", " + country;
    }
}

B.getDescription() is expected to return an extended address but it wouldnt. It would if A.getDescription() was implemented as

        return getName() + " " + getAddress();

2) I personally dont use this for readability because IDE marks this with a different color

Andere Tipps

is it better to use the get method in the same class or the variable itself?

IMHO use the variable. Accessor methods are primarily for other objects to use.

Also I am confused if i should be using the this.anything so much. It seemed easier to read when comparing objects of the same type to each other.

It's not always required for you to explicitly use the this reference..it's mainly used for readability, like you said.

I think that using the getter methods are better for mantainability. Consider the Null Object pattern which a way to achieve is by making this:

public String getName(){
    if (this.name == null){
        this.name = "";
    }
    return this.name;
}

This should save you from checking up a lot of null before operating with the variable.

public boolean isCorrect(){
    if(this.name != null && this.name.isEmpty()){
        //The null check up is boilerplate code
        return false;
    }else{
        return true;
    }
}

I'd rather write this:

public boolean isCorrect(){
    if(this.getName().isEmpty()){
        //The null check up is boilerplate code
        return false;
    }else{
        return true;
    }
}

Of course, this depends if you adopt this pattern.

Also consider that you have

double width;
double height;

public double getWidth(){
    return this.width;
}

but at some point you decide to change it for a class but still have the methods so your program doesn't break down.

Dimension dimension;
public double getWidth(){
    return this.getDimension().getWidth();
}
// etc...

Finally (as commented by MadProgrammer), when you use inheritance, the methods can be overridden to represent better the intended object.

The use of this is not necessary unless you have a case (such as in a constructor) where a parameter has the same name as a field.

For accessing properties, it may be beneficial in the long run to use the public getters, because you may want to add some form of processing to the property, and if you use getters everywhere, you only have to make that change once.

If your get method returns the data with some formatting, you have to use the get method, otherwise, the variable itself will be fine to use.

this is only required if your method parameters are same as your member variables, otherwise, this is not compulsory.

For example:

private String str;

public void setString(String str){
   this.str = str;  // here this.str is necessary because this represents the memeber variable
}

public String getString(){
    return this.str; // here this is optional and you can simply omit it 
}

You can use either the accessor or the variable itself, its one of those personal preference things.

Some people like to use the variable itself because you don't have the overhead of calling a function. But, if you have any restrictions on the values your variables can be, sometimes it is just cleaner to only use your accessors and mutators; especially if you are going to be subclassing. But its one of those things that can go either way.

The way that I like to use the this keyword, is I always use it for instance variables. I find that it makes the code easier to read because you can visually determine where you are using instance variables, and where you are using local variables. Again this is a personal preference thing.

The main thing is to make sure your code is clean, and readable. Also, make sure that you are following whatever coding standard your organization is using, and be sure to be consistent.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top