I am working on a Deck project and I have a Card class with two enums for it:
package exercise4;
public class Card
{
public enum Suit
{
HEARTS,SPADES,CLUBS,DIAMONDS
}
public enum CardValue
{
ACE,TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING
}
private Suit s;
private CardValue cv;
other methods...
}
Ok thats right for Card class, the problem is that I have to call them from another class, but NetBeans doesn't allow me to do this:
Card testcard=new Card(CardValue.ACE,Suit.HEARTS);
and NetBeans force me to do this:
Card testcard=new Card(Card.CardValue.ACE,Card.Suit.HEARTS);
but on test files provided by my teachers they call enums this way and it must work:
package exercise4;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
public class BarajaTest
{
@Test
public void testGetCard() {
int position = 0;
Deck instance = new Deck();
Card expResult = new (CardValue.ACE,Suit.HEARTS);
Card result = instance.getCard(position);
assertEquals(expResult, result);}
}
What am I doing bad? :/
Thanks
You can use CardValue
and Suit
directly by simply importing them, same as any other class/enum/interface.
package stackoverflow;
import exercise4.Card;
import exercise4.Card.CardValue;
import exercise4.Card.Suit;
public class Main {
public static void main(String[] args) {
Card testcard = new Card(CardValue.ACE, Suit.HEARTS);
// more code
}
}
Of course, you could also just make CardValue
and Suit
top-level enums by moving them to their own source files: CardValue.java
and Suit.java
.
As others pointed out, you can also import the enum values statically:
package stackoverflow;
import exercise4.Card;
import static exercise4.Card.CardValue.*;
import static exercise4.Card.Suit.*;
public class Main {
public static void main(String[] args) {
Card testcard = new Card(ACE, HEARTS);
// more code
}
}
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