I'm running to compiler error while I'm trying to use Enum to create a program that display a deck of cards. The compiler mistake is "Cannot find symbol", but I did call the constructor and use method call to call the method in card class. It still give me compiler error. I don't know why.

Here is the code for card class:

public class Card 
{
    public enum Suit{ DIAMONDS, CLUBS,HEARTS,SPADES}
	 public enum Rank
	 {                
	 	ACE,DEUCE,THREE,FOUR,FIVE,SIX,
		SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,
    	KING
	}
	private final Rank rank;
	private final Suit suit;
	

    public Card(Rank rank,Suit suit)
	 {
        this.rank = rank;
        this.suit = suit;
    }

    public Suit getSuit() 
	 {
        return suit;
    }

    public Rank getRank()
	 {
        return rank;
    }
	 @Override
	 public String toString()
	 {
	 	return rank + " of " + suit;
	 }
}

It compile for the card class.

import java.util.*;

public class Deck
{
	private static Card[] cards = new Card[52];

	public Deck()
	{
		Card car = new Card();

      for (Suit suit : car.Suit.values())
		
			for (Rank rank: car.Rank.values())
			
				cards.add( new cards(rank, suit));
			
      
    }

    public Card[] getCards()
	 {
        return cards;
   
}

The compiler error occur in this class. The message is "Cannot find symbol." I have not create a display class yet, but I will. I just need to get my deck class. Where did I do wrong in my code? Thank you so much.

Edited 5 Years Ago by nickliutw: n/a

Your enum is defined inside the card class so you need to qualify its name to use it. Better/easier is to move the enum declaration to be outside the card class.

Your Deck class contains a lot of compile time errors. You can't call "add" on an array in Java. Did you mean to use ArrayList class? Also, there is no reason why enums inside the Card class should be non-static since they anyways are not dependent on the instance of a Card class but are still related to the Card class. Just make them static, import them in the Deck class and you should be good to go.

Just to make sure I understand correct, do I have to declare enum outside the card class or I can just to make them static. Another question I have about the import. I don,t quite understand how do I import the card class to deck class? Can You give some examples? Thank you do much.

You have been presented with two options:
1. Declare the public enums in their own .java files so you can refer to them by their simple names in your other classes in the same package.
2. Keep them where they are, declare them static, and refer to them via their qualified names Card.Rank and Card.Suit (*). ~s.o.s~may have also been suggesting that you could use import static Card.* to make the enum names directly usable in your other classes - I haven't tried that myself, although I know it does work for ordinary public static final variables.

(*) that means all uses of the name, eg lines 11 and 13 there are two uses each, but you only qualified one of each

Edited 5 Years Ago by JamesCherrill: (*)

OK, just checked out the import static - it does work for public enums in a public class, but you can't import anything unless it's in a named package, so
import static myPackage.Card.*;
will let you use the enums without qualifying their names, as I'm sure ~s.o.s~ knew all along.

if I choose option 2 which is just leave the way it is and change it to static, so what I do is

public static enum Suit{ DIAMONDS, CLUBS,HEARTS,SPADES}
public static enum Rank
	{                
	 	ACE,DEUCE,THREE,FOUR,FIVE,SIX,
		SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,
    	KING
	}

Then I little bit lost on what you say about

refer to them via their qualified names Card.Rank and Card.Suit (*)

I don't quite under this part.

There are three ways in which you can access a static public enum from a different package:

  1. Import the enum as a regular import. e.g. import your.pkg.Card.Rank;
  2. Import the enum using a static import. e.g. import static your.pkg.Card.*;
  3. Import the Card class and refer the Rank class the way you would access any other static member of a class. e.g.
    import your.pkg.Card;
    
    // somewhere down in your code
    Card.Rank rank = null;

Another cool thing worth noting is that as per JSL 8.9 and 9.5, nested enums for classes and interfaces are implicitly static but you are free to explicitly specify the static modifier. So:

public class Test {
  public enum Rank { ONE, TWO }
}

// is same as
public class Test {
  public static enum Rank { ONE, TWO }
}
This question has already been answered. Start a new discussion instead.