I have the following class:
public class Card { public enum Suit { SPACES, HEARTS, DIAMONDS, CLUBS }; public Card(Suit nsuit, int nrank) { suit = nsuit; rank = nrank; } private Suit suit; private int rank; }
I want to instantiate it in another class, but that class doesn't understand the Suit
enum. Where should I put the enum to make it publicly visible?
Learn to create Java enum where each enum constant may contain multiple values. We may use any of the values of the enum constant in our application code, and we should be able to get the enum constant from any of the values assigned to it.
String[] digit = {"one", "two", "three"}; String[] teen= {"ten", "twenty", "thirty"}; String[] anchors = {"hundred", "thousand", "million"}; I'm thinking of transferring these to enums separately, so I will have 3 enum classes: digit , teen and anchors with getValue methods implemented.
java file may have only one public class. You can therefore declare only one public enum in a . java file. You may declare any number of package-private enums.
The Suit enum is inside the Card class, and you have to access it that way:
new Card(Card.Suit.SPADES, 1);
Or, you can import the Suit class from within Card, and access it directly:
import my.package.Card.Suit; new Card(Suit.SPADES, 1);
The other option is to put Suit in its own class, as suggested by Bert.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With