0

I'm new to java, but my understanding is that the keyword new comes before a constructor. However, in this example from Oracle's Java Tutorial, this is not the case.

public static int numSuits = 4;
public static int numRanks = 13;
public static int numCards = numSuits * numRanks;

private Card[][] cards;

public Deck() {
    cards = new Card[numSuits][numRanks];
    for (int suit = Card.DIAMONDS; suit <= Card.SPADES; suit++) {
        for (int rank = Card.ACE; rank <= Card.KING; rank++) {
            cards[suit-1][rank-1] = new Card(rank, suit);
        }
    }
}

Card is class, and I do not understand what this line means:

cards = new Card[numSuits][numRanks];

Can someone please explain what this line of code means.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
I.D.
  • 31
  • 2

1 Answers1

1

In this case, new Card[numSuits][numRanks] is instantiating a 2 dimensional array of cards, not the Card class itself. Instantiating the Card class comes later, inside of the 2 for loops as new Card(rank, suit);

Alexa Y
  • 1,854
  • 1
  • 10
  • 13