Example of trying to change an object that was returned

class myInt {
   public int myintdata;

   public myInt(int intval) {myintdata = intval;}

   public int getmyint() {return myintdata;}
}

   
   public class Dizzy {
      private String name;
      private int calories;
      private myInt mycalories;
      

   public Dizzy(String nameOf) {
      name = nameOf;
   }

   public Dizzy(String nameOf, int caloriesOf) {
      name = nameOf;
      calories = caloriesOf;
      mycalories = new myInt(calories);
   }
   
   public String nameOf() { return name; }

   public int caloriesOf() { return calories; }

   public myInt mycaloriesOf() { return mycalories; }
   }

   
   public class DizzyTest {
  
   public static void main (String[] args) {

      Dizzy general = new Dizzy("General",500);
      System.out.println(general.nameOf() + ": " + general.caloriesOf());
      
      myInt iref = general.mycaloriesOf();
      System.out.println(iref.getmyint());

      iref.myintdata = 600;
      System.out.println(general.mycaloriesOf().getmyint());
   }
  } 
         

In this example, DizzyTest retrieves an object of type myInt
from class Dizzy that contains a field myintdata with value 500, 
and it successfully changes that field to 600, even though the field
mycalories in the Dizzy was private.

However, if you change "public int myintdata" to "private int myintdata"
in class myInt, then DizzyTest won't be allowed to make that change.