title: Java: Modifying Objects by Method Call
body: <p>I've read a gazillion times that in Java, arguments passed to methods cannot be modified by the methods. Nonetheless, I find I <strong>can</strong> modify objects I create as opposed to Java Objects. Consider the following code:</p>
  
  <pre><code>    // My Integer Object
      class MyInteger {
          Integer val;
      }
  
      // Change Java Object
      public void test1() {
          Integer intval;      // Integer object
          intval = 123;
          setInteger( intval );       // THIS DOESN'T WORK
  
          TTY.putR( "Integer Object="+intval);
      }
  
      // Change My Object
      public void test2() {
          MyInteger myInt;    // MyInteger object
          myInt = new MyInteger();
          myInt.val = 456;
          setMyInteger( myInt );      // THIS WORKS!
  
          TTY.putR( "MyIntegerObject=" + myInt.val );
      }
  
      // Set an Integer object
      public void setInteger( Integer val) {
          val = 888;
      }
  
      // Set a MyInteger object
      public void setMyInteger( MyInteger myint) {
          myint.val = 999;
      }
  </code></pre>
  
  <p><strong>test1</strong> doesn't work as I have been warned. But <strong>test2</strong> works just fine. AFAIK, <em>both are objects</em> and are passed by reference. So how come one works and the other doesn't? (Note: TTY is my print function)</p>