Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the "this." command needed in this constructor? (java)

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];
    }
}
like image 659
David Avatar asked Mar 22 '10 18:03

David


1 Answers

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".

like image 191
Bozho Avatar answered Sep 22 '22 16:09

Bozho