Andrew Willoughby
Andrew Willoughby

Reputation: 41

Java Access Modifier Best Practice

This may seem a basic question, but I'd like to get this right.

I have a Class 'AWorld'. Within that class, I have a method that draws a border, depending on the map size set by the user.

If the variable 'mapSize' is private, but I want to access it's value from within the same class, is it more appropriate to reference it directly, or use a getter method.

The code below should explain what I'm wanting to know.

package javaFX;

public class AWorld {
    //initialized later
    AWorld newWorld;

    private int mapSize = 20;

    public int getMapSize()
    {
        return mapSize;
    }

    public void someMethod()
    {
        int var = newWorld.mapSize; //Do I reference 'mapSize' using this...
    }
    // Or...

    public void someOtherMethod()
    {
        int var = newWorld.getMapSize(); //Or this?
    }
    public static void main(String[] args) {}

}

Upvotes: 0

Views: 1399

Answers (3)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Either of those is ok since you're getting a primitive field. If the get method does another operation before returning the data e.g. performing a math operation on the value, then it would be better to use it rather than calling the field directly. This is specially meant when using proxy/decorator pattern on your classes.

Here's an example of the second statement from above:

//base class to be decorated
abstract class Foo {
    private int x;
    protected Foo foo;
    public int getX() { return this.x; }
    public void setX(int x) { this.x = x; }
    public Foo getFoo() { return this.foo; }

    //method to prove the difference between using getter and simple value
    public final void printInternalX() {
        if (foo != null) {
            System.out.println(foo.x);
            System.out.println(foo.getX());
        }
    }
}

//specific class implementation to be decorated
class Bar extends Foo {
    @Override
    public int getX() {
        return super.getX() * 10;
    }
}

//decorator
class Baz extends Foo {
    public Baz(Foo foo) {
        this.foo = foo;
    }
}

public class Main {
    public static void main(String[] args) {
        Foo foo1 = new Bar();
        foo1.setX(10);
        Foo foo2 = new Bar(foo1);
        //here you see the difference
        foo2.printInternalX();
    }
}

Output:

10
100

Upvotes: 2

John B
John B

Reputation: 32949

IMHO, if you are referencing a field of the current instance the general rule is to access the field directly with mapSize or this.mapSize.

If you are referencing a value from a different instance (be it of the same class or a different class, I would use the getter method). I believe this would lead to simpler refactoring. It also maintains the contract that any other instance gets the field value via the getter which allows for additional functionality in the getter.

Upvotes: 0

mavroprovato
mavroprovato

Reputation: 8352

You better dereference it directly.

The point of the private modifier is not to expose internal implementation to other classes. These other classes will use the getter method to get the value of the private property.

In your own class, there is no point on using the getter. Worse, someone may have overridden that method in a class that extends your class, and the getter may perform something that you do not expect

Upvotes: 0

Related Questions