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
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.
8 Comments
low hanging fruit :)this.x is different from this, so this.x.f is different from this.f.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.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
this.x.f referring to? Since x isn't initialized to any value...?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.
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
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?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.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
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
thisinthis.bumpnecessary?
No.
Likewise,
this.bump(this.y,x)would be equivalent right?
Yes.