-1

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
Rand Random
  • 7,300
  • 10
  • 40
  • 88
romanocph
  • 38
  • 3

1 Answers1

0

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?

There is not a new hobbit created inside each orc. The two orcs z and u holds a reference (a pointer) to the same Hobbit object - the one you created in this line:

Hobbit y = new Hobbit(1);

This is because in this line:

Ork u = new Ork(z.frodo);

You are creating a new orc with the frodo of z, so the frodo of u will be the same as the frodo of z.

Why is z.frodo.fuss on the last line equals to 0, if it was u.frodo.fuss that was changed to 0?

Since z.frodo and u.frodo are the same object, changes made to any of them will be reflected when you access the other. Here:

u.frodo.fuss = 0;

You've changed u.frodo, which is the same as z.frodo. So when you look at z.frodo.fuss, you get 0.

Note that you didn't do much to x, other than passing it to zweiTuerme.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Hi Sweeper, i understood it, but it gave me another question. Why will System.out.print(y.fuss + " x "); print 14 and not 0? Isnt Ork z = new Ork(y); creating an orc z with the hobbit y? So shuldnt y.fuss also change when we change u.frodo.fuss, the same way as z.frodo.fuss changed? And why is x.nase changing after the methode zweiTuerme is called, but x.fuss isnt? Thanks agan, Pedro. – romanocph Jan 30 '18 at 17:00