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 ge
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();
}