Search code examples
javaenumsarraylistplaying-cards

Initializing ArrayList in a test driver


I'm trying to initialize a new instance of the ArrayList defined in my playingCard.java file:

import java.util.ArrayList;

public class PlayingCard 
{

    public enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
        Jack, Queen, King, Ace}

    public enum Suit { Spades, Diamonds, Hearts, Clubs }

    private final Value value;

    private final Suit suit;

    /**
    * Constructs a card with specified initial value and suit
    * @param value
    * @param suit 
    */

    public PlayingCard(Value value, Suit suit) 
    {
        this.value = value;
        this.suit = suit;
    }

    /**
     * Retrieves the value of a card
     * @return value
     */

    public Value getValue() 
    { 
        return value; 
    }

    /**
     * Retrieves the suit of the card
     * @return suit
     */

    public Suit getSuit() 
    { 
    return suit; 
    }

    /**
    * Custom toString
    *@return value and suit as a string.
    */

    @Override
    public String toString() 
    { 
        return "PlayingCard[value=" + value + ", suit=" + suit + "]"; 
    }

    /**
     * Format method to print out the value and suit of a card.
     * @return value and suit as a string.
     */

    public String format()
    {
        return value + " of " + suit + ", ";
    }

    /*private static final List<PlayingCard> deck = new ArrayList<PlayingCard>();

    // Initialize deck
    static 
    {
        for (Suit suit : Suit.values())
        {
            for (Value value : Value.values())
            {
                 deck.add(new PlayingCard(value, suit));
            }
        }
    }*/
}

If the last 12 or so lines aren't commented out, there is no problem with the code. However I want to initialize the deck in a separate test driver and receive 2 errors when copying the code over. The test driver currently looks like this:

import java.util.ArrayList;

public class PlayingCardTester 
{
public static void main (String[] args)
{
    static  List<PlayingCard> deck = 
        new ArrayList<PlayingCard>();

    // Initialize deck
    static 
    {
        //for ea PlayingCard.Suit suit in PlayingCard.Suit.values()
        for (PlayingCard.Suit suit : PlayingCard.Suit.values())
        {
            for (PlayingCard.Value value : PlayingCard.Value.values())
            {
                deck.add(new PlayingCard(value, suit));
            }
        }

    }
}
}

I have an error on line 14 of the test driver

static  List<PlayingCard> deck = new ArrayList<PlayingCard>();

saying it's an illegal start of expression. I've tried using different keywords in front of the statement and the error stays the same. The second error is the last bracket which just says "null". I am new to using enums, so it could be something very simple which I've over looked...


Solution

  • You don't need static declaration in static method.

    List<PlayingCard> deck = new ArrayList<PlayingCard>();
    

    Also there is no need of Static Block since you are already in static context.


    References:

    1. Static Initialization Blocks