I am trying to understand the following Java code, but I don't get it what happens on the two lines where the object orc u and orc z are created. As far as I know, there is a hobbit created inside each orc, but is it a new object or does it work as a pointer? Why is z.frodo.fuss on the last line equals to 0, if it was u.frodo.fuss that was changed to 0?
public class Hobbit {
public int fuss
public int[] nase = {6}
public static int ohr = 5;
public Hobbit (int f) {
fuss = f + ohr++;
}
}
public class Ork {
public Hobbit frodo;
public Ork (Hobbit sam) {
frodo = sam;
}
}
public class Mittelerde {
public static void zweiTuerme (int[] a, int b) {
a[0] = a[0] * 4;
b = a[0] + 3;
System.out.println("T: " + (a[0] + b));
}
}
public static void main (String[] args) {
Hobbit x = new Hobbit(2);
Hobbit y = new Hobbit(1);
Ork z = new Ork(y);
y = new Hobbit(x.fuss);
Ork u = new Ork(z.frodo);
u.frodo.fuss = 0;
zweiTurme(x.nase, x.fuss);
System.out.print(x.fuss + " x ");
System.out.print(x.nase[0] + " x ");
System.out.print(x.ohr + " x ");
System.out.print(y.fuss + " x ");
System.out.print(y.nase[0] + " x ");
System.out.print(y.ohr + " x ");
System.out.print(z.frodo.fuss + " x ");
}
So can anyone help me to understand it?
It would be also nice if someone could show me how a orc object would look like.
I know that the hobbit x would look like this, after Hobbit x = new Hobbit(2);
- x.fuss = 7
- x.nase = {6}
- x.ohr = 6