/** * Shallow copying in Java * As a reference model language (for classes) when you make a copy * of an object in java, that copy is shallow; ie the only thing that * happens is you get a new reference to same memory location. */ public class SCopy { // A class containing just an int private class AA { int theInt=0; } // A class containing just a reference to another class. private class BB { AA theAA = new AA(); } // Example of shallow copy in Java public void test() { BB bb1 = new BB(); BB bb2 = bb1; bb2.theAA.theInt = 42; System.out.format("BB2 value of theInt in theAA:%d\n", bb2.theAA.theInt); System.out.format("BB1 value of theInt in theAA:%d\n", bb2.theAA.theInt); } public static void main(String[] args) { new SCopy().test(); } }