frazman
frazman

Reputation: 33273

"this" vs normal initialization java

I am a beginner in java and has slowly started to get the feel of this language.

I am having a hard time understanding "this" variable.

For example, which is better.. and why?

  public class Foo{
   int a;
   public Foo(int _a){
   a = _a;
     }
 }

vs

public class Foo{
   int a;
   public Foo(int _a){
   this.a = _a;
     }
 }

Thanks

Upvotes: 1

Views: 112

Answers (5)

daniel gratzer
daniel gratzer

Reputation: 53901

From the code you posted above, there is no difference. However sometimes Java's scoping and shadowing mechanisms make this necessary. For example while

public class Foo{
   int a;
   public Foo(int _a){
       this.a = _a;
   }
 }

is unneccesary, this is a

public class Foo{
   int a;
   public Foo(int a){
       this.a = a;
   }
 }

which is sometimes useful when instead of simple variable names like a, you have more complicated ones.

This may be a weird phenomenon for some C++ types were calling this->something actually does do something different with how the variable is looked up. However Java sidesteps all of these details.

Upvotes: 2

duffymo
duffymo

Reputation: 308918

Personally, I hate those conventions that use the underscore or "m_" to distinguish variables. That's the stuff that IDEs are meant to make clear.

You use this to clearly delineate variables that belong to the current instance.

Neither of your examples have any advantage. You can prove it to yourself by looking at the byte code that's produced. If you use javap, you'll see that the two compile to identical code.

It has more to do with readability for yourself and other programmers in my opinion. And good IDEs, like IntelliJ, use highlighting and color coding to make it even clearer.

The one circumstance where it's absolutely required is when there's a name collision between a method parameter and a private member. Then you have to use this to disambiguate which is which for the compiler:

public class Foo {
    private int x;
    public void setX(int x) {
        this.x = x;  // this is required here.
    }
}

Upvotes: 8

Ptharien's Flame
Ptharien's Flame

Reputation: 3246

In this case, they generate identical bytecode, so it's really just a matter of style which one you pick. In general, this (which is not strictly a variable) refers to the class currently being specified, or, more usually, one of its instances (the one that had the currently-being-specified method called on it).

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 882028

Which is better is a subjective thing. I prefer the explicit naming this.a (though my variables would be a little more descriptive than that) since it makes it clear that what you're referring to belongs to the current object.

Otherwise, you have to go looking elsewhere to confirm. For example, it could be a local variable created anywhere between this point and the start of the function.

Upvotes: 3

Jim Garrison
Jim Garrison

Reputation: 86774

Neither is "better" as far as the language goes, they are exactly equivalent.

However, there's a good reason to prefer the second option, which is that the code is more explicit about what you're doing.

Upvotes: 4

Related Questions