This might be a little too basic question. I have tried the ways already given but its not working. I want to call an ENUM from another class.
package com.blackjack.game.cards;
public enum Card implements Comparable<Card> {
ACE(1, 11),
KING(13, 10),
QUEEN(12, 10),
JACK(11, 10),
TEN(10, 10),
NINE(9, 9),
EIGHT(8, 8),
SEVEN(7, 7),
SIX(6, 6),
FIVE(5, 5),
FOUR(4, 4),
THREE(3, 3),
TWO(2, 2);
public final int rank;
public final int value;
private static final String[] CARD_NAMES = {
"joker", "ace", "two", "three",
"four", "five", "six", "seven",
"eight", "nine", "ten", "jack",
"queen", "king"
};
private Card(final int rank, final int value) {
this.rank = rank;
this.value = value;
}
public int getRank() {
return rank;
}
public int getValue() {
return value;
}
I am doing it in the following way but it's not working.
import com.blackjack.game.cards.Card; Card card = new Card.ACE;
In Java You don't need to use new to access the enum value:
Card card = Card.ACE;
Another note: Since you have public getters, why to not use private fields:
private final int rank;
private final int value;
And yet another note, it looks like CARD_NAMES are not used anywhere and they're private.
The enum can contain the name in a String field:
private final int rank;
private final int value;
private final String name;
public String getName() {return name;}
When you create an enum:
ACE(1, 11, "ace"),
KING(13, 10, "king")
....
Alternatively, you can access the names of created Enums via built-in name()
method; here is an example:
String name = Card.ACE.name();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.