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];
}
}
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With