0

When using instance variables within a class, is there any advantage to directly referencing the instance variable versus using a getter? Which is more commonly seen?

Example using method calls:

public double howMuchFreeSpace() {
    return getCapacity() - getVolume();

Example referencing instance variables:

public double howMuchFreeSpace() {
    return this.capacity - this.volume;

1 Answers1

0

It's usually matter of preference inside a class. Direct referencing is more readable IMHO. So I suggest using direct referencing unless you need to execute some code when getting the value - that may be a reason to use getter.

Abdul
  • 537
  • 1
  • 5
  • 21