I’m going through a book on Java and I’ve been understanding most of it so far. However, I’ve run across some code that I can’t seem to figure out. This is from a simple Blackjack game:
public class CardDeckTest {
public static void main(String args[]) {
CardDeck deck = new CardDeck();
System.out.println("Deck Listing:");
deck.list();
Card card = deck.deal();
System.out.println("Dealt " + card);
card = deck.deal();
System.out.println("Dealt " + card);
System.out.println("Top index: " + deck.getTopIndex());
deck.reset();
System.out.println("Reset deck... Top index: " + deck.getTopIndex());
card = deck.deal();
System.out.println("Dealt " + card);
System.out.println("The last card is " + deck.getCard(deck.getNumCards() - 1));
}
}
The confusing line to me is Card card = deck.deal(); So far I’ve seen lines similar to the second one, with a “new” in it. What is this line doing? Why isn’t there a “new” there.
I believe it’s referring to this in CardDeck
public Card deal() {
Card dealt = cards[top];
top ++;
if (top >= cards.length) reset();
return dealt;
}
Thank you!
It is invoking the deal method on a specific instance of CardDeck, and returning an instance of Card, which is being stored in the local variable card. Does that help?