0

My query is, can a getter method ever return a wrapper data type? Will it violate the principle of Encapsulation? Why not? Is immutability of classes a part of encapsulation?

public class TestGetter{
private Integer a;
public TestGetter(int _a){
    a = _a;
}
public Integer getA(){
   return a;
      }
 }

In Some client code:

TestGetter tg  = new TestGetter(5);
Integer corruptX = tg.getA();//This should return the reference
corruptX = null;

So now a = null?

When i run it i dont see a as null.

hakish
  • 3,990
  • 7
  • 39
  • 56
  • 1
    This code won't compile. The `TestGetter(int)` constructor requires an argument. – erickson Jan 07 '12 at 02:06
  • possible duplicate of [Is Java pass by reference?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – erickson Jan 07 '12 at 02:21

4 Answers4

3

This is no different to:

Object a = new Object();
Object b = a;
b = null;
// a is not null here

Modifying a reference is not the same as modifying the object being referred to.

To answer your question about encapsulation; yes, it's ok to return a reference to an Integer member variable, because Integer instances are immutable.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • @hakish: If the member variables were mutable, then the user has the ability to modify the internal state of your object via the getter, which is bad. – Oliver Charlesworth Jan 07 '12 at 02:26
3

As @Oli Charlesworth mentioned, it is not different to:

1. Object a = new Object();
2. Object b = a;
3. b = null;

and here is the graphical representation to understand how the reference works in Java:

After line 1:

enter image description here

After line 2:

enter image description here

After line 3:

enter image description here

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

Your test is telling you why it's no problem to return a reference to an object--Java does not pass values by reference, so even though you reassign corruptX this has absolutely no effect on the data in tg.

That doesn't mean there aren't issues with passing internal members of an object to outside callers. If the objects you pass back are not immutable, then you cannot protect the invariants of your class since someone can change the internals of the object you give back.

For example,

//immutable point
public class NdPoint {

    List<Double> coordinates = new ArrayList<Double>(3);

    public List<Double> asCoordinateList() {
        return coordinates;
    }
}

//...
NdPoint myPoint = new NdPoint(x, y, z);
myPoint.coordinateList().set(0, xPrime);

This class claims to be immutable but because it does not defensively copy members that it passes outside, it actually is not immutable. It could retain immutability like this:

public List<Double> asCoordinateList() {
    return new ArrayList<Double>(coordinates);
}
Mark Peters
  • 80,126
  • 17
  • 159
  • 190
0

This happens because Java Object values are just passing a reference of the object rather than the object itself

JRideout
  • 1,635
  • 11
  • 16