Java Conventions: use getters/setters WITHIN the class?

My professor really emphasizes protecting against privacy leaks by always using accessors and mutators to access private instance variables; however, do I have to use the getters/setters of a class within the class?

So for instance, if I have the following class:

public class Person 
{
    private String name;
    private int age;
}

and I want to write a toString() method for it. Can I just write:

public String toString()
{
    return name + " " + age;
}

OR do I need to do something like this:

public String toString()
{
    return this.getName() + " " + this.getAge();
}

You CAN do either one. However, your professor might appreciate using the methods instead of the direct access. Here's why.

Let's say you have a class like this:

class SomeClass {
    private int someValue;
    private String someString;

    public SomeClass(int someValue, String someString) {
        this.someValue = someValue;
        this.someString = someString;
    }

    public int someValue() {
        return this.someValue;
    }

    public String someString() {
        return this.someString;
    }

    public String toString() {
        return someValue + ": " + someString;
    }

}

It's pretty straightforward, right? Well, what if all of a sudden we want to CHANGE the implementation of how we calculate someValue, and base it off of someString:

public int someValue() {
    int value = 0;
    for(int i = 0; i < someString.length; i++) {
         if(someString.charAt(i) == ' ') value++;
    }
    return value;
}

Now you also have to change every place where variable someValue was used.

So if you want to make the code easier to maintain in the long run, use the methods calls. This way when you code changes on you (and trust me, it changes all the time) you only have to change it in one spot instead of two.

And yes, you would want to use a method call in getting someString instead of the direct access in the last method :-)


When I design a class I try to make a clear distinction between the inside (implementation details) and the outside (the interface exposed to the world). Getters and setters provide a convenient place to convert values between the form in which they are stored in the object’s instance members and the form in which the outside world sees them. Using getters and setters internally would muck that up, because they'd be getting used by both the inside and outside.

If you find yourself wanting to hide part of a class from another part of the same class, consider breaking off the part you want to hide into its own class.


It's normally not a good idea, for a number of reasons:

  • You may not even want accessors for all fields
  • Some accessors may make a defensive copy so not to expose internal state, this is normally unnecessary within the class where you know that you are not going to modify it - or plain wrong if you know you ARE going to modify it
  • It makes debugging more annoying, because you have to follow the getters / setters
  • It makes reading the code harder in most IDEs, since most of them color fields differently than local variables

... but as always, there are exceptions. Some setters may have side-effects (for example setting a second value) that you WANT to execute, then it might be better to use the setter. Also, if you design your class for inheritance, it may be better to go via an accessor if you want the subclass to be able to alter the behavior.


In general, no. If your getter returns something other than the value of the field then you should use the method, but in that rare case your method should have a more descriptive name. For a bad example, if you have:

public void setName(String name)
{
  _name = name;
}

and your getter returned something else, like

public String getName()
{
  return _name.toUpperCase();
}

then yes, you should use the getter. It would be better, though, to have a more descriptive name for that getter:

public String getNameAsUppercase()
{
  return _name.toUpperCase();
}

You can use the accessors and mutators, but its a messy standard to follow.

It clutters up your code and confuses anyone trying to read it thinking it might not be a part of your class.

Basically, just access the variables directly from inside your class, and indirectly from anywhere else.