0

I do not understand why a property should be made private when there also are a public getter and setter.

class C{
  private int i;

  public int getI(){return i;}
  public void setI(int i){this.i=i;}
}

Now the private property i is made public. It is as if you had done

class C{
  public int i;
}

Or do I not understand something?

ericj
  • 2,138
  • 27
  • 44
  • 3
    *the property becomes public* - Well, **access to that property becomes public**. We will have more control over the way the property is accessed. – TheLostMind Jan 14 '16 at 06:37
  • No, because your `setI` could have logic which could control the way in which `i` get's changed, you have no such control with your second example – MadProgrammer Jan 14 '16 at 06:59
  • I understand, but Eclipse generates the getter and setter as above, and almost always it is kept this way, and I was wondering what the use of this pattern is. – ericj Jan 14 '16 at 15:22

0 Answers0