I have a class that uses enumerable data types. In it’s constructor, it uses two of these. When I try to instantiate an object of this class from another file, I get an error.
Here is part of the code from the class:
public class Card {
public static enum colorType {BLACK, RED};
public static enum suitType {CLUB, DIAMOND, HEART, SPADE };
public static enum rankType {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
NINE, TEN, JACK, QUEEN, KING };
private boolean faceup = false;
private rankType rank;
private suitType suit;
//*************************************************************
//Card- constructor. initializes a card and makes it face down
//*************************************************************
public void Card(rankType r, suitType s){
this.rank = r;
this.suit = s;
this.faceup = false;
}
When I try to do this:
Card C1 = new Card(ACE,SPADE);
from another file, I get an error. using rankType.ACE and suitType.SPADE as arguments also gives the same error. I can do Card C1 = new Card(); with no errors, but that would create a card with nothing in it. The exact error I’m getting is:
internal error; cannot instantiate Card.<init> at Card to ()
Obviously there is some trick to using enumerables in this situation. What am I doing wrong?
Fixed. Solutions to my problems are in posts from uthomas, Tieson T., and the accepted answer.
As the enum is declared within a class, is a inner type so you must qualify using the outer class:
Could be better if you define the enums in separate classes so can be referenced (the type) directly and using static import like
import static RankType.*you could use:Also remove
voidfrom constructor as stated.