Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access enum from another class

Tags:

java

enums

I am new to Java and I am struggling to get my Enumeration to work. I am working in BlueJ and I have 2 classes (trying to build a game of solitaire). my first class is called Card and inside this class I have an enumeration called Suit. My other class is Deck and I am trying to call the enumeration from this class. My problem is in the for loop for deck where I have declared Suit suit. It does not recognize the class Suit.

My code is below, if anyone can tell what I am doing wrong... it would be much appreciated. Thanks!

public class Deck
{

private Bag<Card> cardBag = new Bag<Card>();

public Deck()
{
  for(Suit suit : Suit.values())
     {
         Card card= new Card (suit, 5);
     }
 }

//

public class Card
{
public enum Suit
{
    H, S, C, D
}

private Suit suit;
private int valueOfCard;

public static final int ACE = 1;
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;

public Card(int valueOfCard, Suit suit)
{
    this.valueOfCard= valueOfCard;
    this.suit=suit;
}
}
like image 453
Alex G Avatar asked Feb 09 '13 22:02

Alex G


1 Answers

There are at least two possible solutions here:

  1. Place the Suit enumeration in its own file.

  2. Access Suit with its fully qualified name in other classes. That is use Card.Suit, rather than just Suit.

like image 76
Code-Apprentice Avatar answered Sep 20 '22 17:09

Code-Apprentice