views:

283

answers:

10

I'm reading a book about java. It just got to explaining how you create a class called "deck" which contains an array of cards as its instance variable(s). Here is the code snippit:

class Deck { 
    Card[] cards; 
    public Deck (int n) { 
        cards = new Card[n]; 
    } 
} 

why isn't the this. command used?

for example why isn't the code this:

class Deck { 
    Card[] cards; 
    public Deck (int n) { 
       this.cards = new Card[n];
    }
}
+12  A: 

this. is implicit.

In general, it's a best practice (at least I consider it one) to only use this when absolutely necessary. When you have a local variable named cards and a member variable named cards, for example, you'll need this.cards to refer to the member variable, as cards refers to the local variable.

In such a case, this is a good idea (although it might be a better idea to rename the member variable).

In every other case, where an implicit this can work, use it.

Randolpho
can this. always be omitted then? or do you mean that the author intends it to be there but isn't writing it because its implicit relative to the reader?
David
@David: It can always be omitted unless you also have a local variable with the same name (as then `cards` on its own would be the local variable not the instance variable, and you'd need `this.cards` to explicitly reference the instance).
Chris Smith
@David, @Chris Smith: Heh... I was editing as you commented.
Randolpho
why should one only use this when absolutely necessary?
David
Because it's implicit and everyone should know that? It's also extraneous; 5 extra characters that do not help readability in the slightest. Well, *I* consider it thus... obviously that's subjective.
Randolpho
+1. Unnecessary 'this.' is codejunk.
Carl Manaster
+1  A: 

The "this" keyword is redundant in that case. cards is already defined at class scope and the complier knows that the member is part of "this" class.

Paul Sasik
+2  A: 

This is implied.

Let's elaborate:

class Deck { 
     Card[] cards; 
     public Deck (Card[] cards) { 
          this.cards = cards; 
     } 
} 

In this case you pass in an array of cards that shares the same name as the object's array of cards. This refers to your object's datamember, not the constructor's parameter.

Bryan Denny
A: 

"this" is implied. There is no need to use "this". I usually put it there just to make the code readable, but again it's not required.

Felix Khazin
+19  A: 

Because there is no ambiguity. There is only one cards variable. this would be needed if there were two - one of which being an instance variable (part of the class, as it currently is), and the other - an argument of the constructor.

And btw, this isn't a "command". It's a "keyword".

Bozho
+3  A: 

When you use the identifier cards in the constructor, the compiler notices that the field (a.k.a. member variable) cards is in scope, and uses it. this.cards would only be necessary to resolve ambiguities, for instance if you also had a local variable or parameter named cards.

Will
+3  A: 

It would have sense if you also have a parameter named cards. Then this.cards would specify that you mean exactly field cards of class, not the parameter.

But anyway it's counted a good practice to use this. in cases as described in your example.

Roman
No, it’s not good practice to add visual noise to your source code.
Bombe
@Bombe: That depends on who you ask. *Some* people consider using `this.` to be good practice, while others don't.
Daniel Pryden
Indeed. So, it’s not a good practice because there are no good practices, anyway. :)
Bombe
+4  A: 

The object's this reference is implied, but it can be useful for clarity (and it's necessary for disambiguation between an object's member and a local variable of the same name, as in the constructor below):

   public class Foo {
       final private int x;

       public Foo(int x) { this.x = x; }

       public int getX() { return this.x; }
   }
Jason S
+7  A: 

You do not need to qualify all access to members with the this keyword. You only need to use it whenever another variable is hiding the member method.

And that is not a feature limited to the constructor, but available in all methods:

public class Test 
{
   private int member;
   private int value;
   public Test(int value) {
      member = 5;
      this.value = value; // required to differentiate from the parameter
   }
   public void f( int member ) {
      int value = 5
      this.member = value; // assign local 'value' (5) to member 'member'
      this.value = member; // assign parameter 'member' to member 'value'
   }
}
David Rodríguez - dribeas
A: 

I try to select variable names in a way that doesn't require the "this" keyword. That is, if I have a property named "value", I will pass to a parameter called val.

A good use of the "this" keyword is for constructor overloading. Consider the following (I will use this. to demonstrate the difference):

public class Person
{
   int age;
   public Person(int age)
   {
      this.age=age;
   }
   public Person()
   {
      this(25); //default age
   }
}

When at all possible, I will try to avoid having the property and parameter name the same to avoid what you see on line 6. However, the use of the this keyword for calling another constructor is a good way to prevent code duplication. This is a trivial example, but when you start finding yourself with constructors that do a lot of the same work with small differences between them, it comes in quite handy.

baultista