I have been playing around with Java and cannot quite understand how java methods work with objects which are passed to them.
For example, In the code below i create some "Container" object instances which contains another object and a primitive. When i pass this "Container" object to methods, I can change the object that is held inside the container instance either directly modfying its value or using the new operator to construct a new object and replace its original. These changes are permanent as their values are that of the new objects when examined outside the method.
What is confusing me greatly is that although i can change a Containers inner object via methods, I cannot actully change the container itself. By this i mean that if i pass a container to a method and try to alter it via swapping or assignment from the new operator.
Below is the code i use to test the modification of an object instances attributes and then modification of the actual instance itself.
class InsideRef{
char myChar;
InsideRef(char newVal){
myChar = newVal;
}
}
class Container{
InsideRef myInRef = null;
int myPrimitive = 0;
Container(char innerChar, int innerPrim){
myInRef = new InsideRef(innerChar);
this.myPrimitive = innerPrim;
}
public void myDetails(){
System.out.format("Container.%s => myPrimitive -> %d || myInRef => %s -> %c.%n",
this.hashCode(),this.myPrimitive,this.myInRef.hashCode(),this.myInRef.myChar);
}
}
class AttribRefModder{
public static void ModObjRefVal(Container toEdit){
toEdit.myInRef.myChar = 'Z';
}
public static void ModNewObjReference(Container toEdit){
toEdit.myInRef = new InsideRef('Y');
}
}
class RefSwapper{
public static void RefSwap(Container A, Container B){
System.out.println("Swapping....");
System.out.print("OBJECT A -> ");
A.myDetails();
System.out.print("OBJECT B -> ");
B.myDetails();
Container temp = A;
A = B;
B = temp;
System.out.print("SWAPPED A -> ");
A.myDetails();
System.out.print("SWAPPED B -> ");
B.myDetails();
System.out.println("Exiting....");
}
public static void RefNew(Container A){
System.out.println("Assigning Reference New Object....");
A = new Container('V',999);
System.out.print("NEW C REF -> ");
A.myDetails();
System.out.println("Exiting....");
}
}
public class ReferenceModding{
public static void main(String[] args){
System.out.println("-----------MODDING INNER REFS----------");
Container C1 = new Container('A', 111);
System.out.print("ORIGINAL A -> ");
C1.myDetails();
AttribRefModder.ModObjRefVal(C1);
System.out.print("MODDED A.Ref -> ");
C1.myDetails();
AttribRefModder.ModNewObjReference(C1);
System.out.print("NEW A.Ref -> ");
C1.myDetails();
System.out.println("----------SWAPPING REFERENCES----------");
Container C2 = new Container('B',222);
RefSwapper.RefSwap(C1, C2);
System.out.print("OBJECT A -> ");
C1.myDetails();
System.out.print("OBJECT B -> ");
C2.myDetails();
System.out.println("----------ASSIGN NEW OBJECTS----------");
Container C3 = new Container('C',333);
System.out.print("OBJECT C -> ");
C3.myDetails();
RefSwapper.RefNew(C3);
System.out.print("OBJECT C -> ");
C3.myDetails();
}
}
I apologise if this is too much code i have posted. It's just i've been playing with Java all day and this object parameter business has really confused me. I cant work out why Java methods allows me to edit and assign a new object to the InsideRef
refrences that are held inside a container class but do not allow me to perform the same operations on the actual container classes.
Thanks for any help you may be able to provide.