0

I read this article: Combination of getter and list modification in Java

and now I wonder if a modification on all types of class attributes is possible only by access via a getter. I tried with an Integer object

public class SomeClass
{
     private Integer someInteger = 9;
     public Integer getSomeInteger()
     {
         return someInteger;                          // id = 18, value =  9
     }
}

Now I try to modify someInteger in another Class:

SomeClass someClass = new SomeClass();
Integer someInteger = someClass.getSomeInteger();     // id = 18, value =  9
someInteger += 1;                                     // id = 26, value = 10
Integer anotherInteger = someClass.getSomeInteger();  // id = 18, value =  9

With a debugger I inspect the object id. In my calling class someInteger first has the same ID as in the getter. After adding a 1, someInteger get a new id and the original class attribute is not edited.

Okay my try with adding a number failed, but is there a possibility to modify the originally object?

I asked me about the difference between the linked example with a List and my example with an Integer. My idea is, that

someInteger += 1; 

internally creates a new Integer object. In contrast the list has its own modification methods, which not causes the instantiation of a new List object. Is this right? From this it would follow that all data types with self modifying methods that I can access by a getter have to be protected from modifying by other classes?

Community
  • 1
  • 1
Karina
  • 197
  • 1
  • 3
  • 9
  • http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value should answer your question. Your local `Integer someInteger` is not the same reference as the one in the class. Modifying your's does not touch the other. And since `Integer` is immutable, you'll create a new object by doing "+1" (should be equivalent to `someInteger = Integer.valueOf(someInteger.intValue() + 1)` due to auto boxing / unboxing) – zapl Nov 19 '14 at 10:29

1 Answers1

1

Your issue is that you are not implementing a proper setter, and Integer is immutable.

So what you are doing when incrementing someInteger right now is incrementing the value of reference someInteger scoped to the method where you are executing the increment, without impacting on the private field.

A setter method takes an argument, and internally sets the field's value, after validation if applicable.

So your setter should look like:

public void setSomeInteger(int value) {
    // TODO validate if applicable
    someInteger = value;
}

Then you would invoke it as:

someClassInstance.setSomeInteger(42);
Mena
  • 47,782
  • 11
  • 87
  • 106
  • Thanks for your fast answer. Please read the linked article. I'm afraid of modifying class attributes only with a getter. – Karina Nov 19 '14 at 10:27
  • @Karina if your getter returns mutable objects (such as a `List`), it is possible to modify it by invoking the getter. This is not possible for `Integer`. – Mena Nov 19 '14 at 10:29