4

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.

Doug B
  • 175
  • 1
  • 1
  • 7
  • 6
    Personally, I think you should use any getter that is public or protected, as it is possible for future developers to override the methods and change there results, which may no longer be reflected by the private field represented by the getter – MadProgrammer Jan 24 '13 at 04:20
  • @MadProgrammer I borrowed your comment for my answer, if that is not ok, let me know and I will edit. – Roger Jan 24 '13 at 04:46

6 Answers6

3

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.

mre
  • 43,520
  • 33
  • 120
  • 170
3

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.

Roger
  • 2,912
  • 2
  • 31
  • 39
  • Thank you for the efficiency tips! I am still pretty new and although i can code a bunch, this stuff is gold to me. I just researched this method further, and will most likely use it in the near future. – Doug B Jan 24 '13 at 05:19
2

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

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • So many different opinions, which is awesome but im trying to understand each perspective and get it right. When i call the second (not getter) method from the subclass type, it will use the newly formatted data, although the method is in the superclass. However when I call the method of the superclass type, it will use the previous version. – Doug B Jan 24 '13 at 05:40
  • See my update with explanation, hope it helps to understand what I mean – Evgeniy Dorofeev Jan 24 '13 at 06:38
0

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.

Alex Gittemeier
  • 5,224
  • 30
  • 55
0

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 
}
zdesam
  • 2,936
  • 3
  • 25
  • 32
0

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.

Dustin F
  • 166
  • 6
  • Great Tips! This is a new instructor so I'm trying to make sure I do everything perfect and as efficient as possible to stand out. The assignment doesn't require a subclass however making it more open to a child class would definitely make it look good on my part. The way i'm understanding it is it is a trade off from overhead of calling a method and ease of adapting it for future use. – Doug B Jan 24 '13 at 04:56