2

I'm trying to understand the following valid Java code. I have two questions, which are commented in the code.

class C {
  C x;  //1.) why is something like x = new C(); not required here?  Does this 
        //mean x refers to this current class of C?
  int f;
  void run(boolean b, int x) {
    C y;
    y = new C(); 
    if (b) { this.bump(y,x); } //2.) Is "this" in this.bump necessary?  Likewise,
                               //this.bump(this.y,x) would be equivalent right?
    else { f = this.x.f + 1; }  
  }
  void bump(C z, int j) {
    z.f=j;
  }
  }

Thanks, sorry for such rudimentary questions

6 Answers 6

10

1.) why is something like x = new C(); not required here?

x is only an uninitialized reference. If you want to use it, you indeed need to assign something to it.

2.) Is "this" in this.bump necessary?

No. this refers to the current object, and since x is a member of the current object you do not need it in this case.

2a) Likewise, this.bump(this.y,x) would be equivalent right?

No. y is a local variable, you can not access it with this.y.

Sign up to request clarification or add additional context in comments.

8 Comments

quick fingers yields you +1.
Its been a low hanging fruit :)
this.x is different from this, so this.x.f is different from this.f.
this.x is that C x; that was not initialized earlier (so lets hope someone does initialize it before calling the run() method). Same goes for the f=this.x.f +1; the int f inside the x needs a value first otherwise the call of the run() method should produce a nullpointer
The code as provided would throw an exception, but perhaps x would be set somewhere else in the code, if this were a real piece of code...in which case, the meaning of this.x.f is still going to be different from this.f.
|
3

This one:

 C x;

declares a single field called x within class C of type C. The statement doesn't set any initial value of the field x. And it's basically the same as C x = null;

That one:

 this.bump(y,x);

this is excessive here. You would get the same with just bump(y,x);

this.y wouldn't compile in this case, because y is a local variable (within the method), not a field.

1 Comment

Thanks for your answer. What exactly is this.x.f referring to? Since x isn't initialized to any value...?
0

C x; means that you've declared a variable of type C called x. You did NOT create an instance yet which you do by calling x = new C();

Using the this keyword isn't necessary when calling a member method from within the same class.

this.bump(this.y,x) is NOT what you think it is, since y is a local variable (local to the run method) and not a class member so you'd actually get a compilation error.

2 Comments

If we didn't create an instance of C x, how is this.x.f legal code?
it'll compile since the compiler doesn't know that you didn't initialize it yet. If you ran the code, however, you'd get a NullPointerException when you got to this.x.f.
0

1.) C x doesn't reference anything here. But if you assign it, it will reference to some class of type C. This might be the current class, but there could also be another one referenced. For example if C is a class Person, x could reference this persons mum, dad, friend, etc...
2.) No, it's not needed there since theres no local variable named the same way. Its equivalent.

2 Comments

What exactly do you mean when you say C x doesn't reference anything here? When we use this.x.f, since C x isn't referencing anything, how is this legal code?
At the line you commented on it, C x has a null reference. A call like this.x.f (the class member, not x.f (the local variable named the same)) would result in a NullPointerException, because you havent assigned an instance of C to your this.x. This will happen at runtime though.
0

1) new allocates memory on the heap instead of the stack

2) this in this.bump is not necessary. this.bump(this.y,x) would not be equivalent because class C does not have a member variable called y

Comments

0

1.) why is something like x = new C(); not required here?

By default, the reference is null. It's up to the programmer to decide whether the reference needs to be initialized to point to some object.

Does this mean x refers to this current class of C?

Here, x is a reference to some instance of class C. The reference can be (and is) null.

2.) Is this in this.bump necessary?

No.

Likewise, this.bump(this.y,x) would be equivalent right?

Yes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.