简体   繁体   中英

Basic questions about Java syntax

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

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 .

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM