Reputation: 14963
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];
}
}
Upvotes: 7
Views: 1855
Reputation: 1740
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.
Upvotes: 0
Reputation: 597422
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".
Upvotes: 20
Reputation: 27596
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.
Upvotes: 2
Reputation: 208466
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'
}
}
Upvotes: 8
Reputation: 189906
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; }
}
Upvotes: 4
Reputation: 66226
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.
Upvotes: 3
Reputation: 5537
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
.
Upvotes: 3
Reputation: 1487
"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.
Upvotes: 0
Reputation: 81557
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.
Upvotes: 1
Reputation: 56448
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.
Upvotes: 12