super95
super95

Reputation: 103

Parameterized enum passed in another enum

I have these two enumeration classes, where Item contains items with their descriptions. The enumeration class Character has four different Character objects: LAURA, SALLY, ANDY and ALEX, each parameterized with a description and an item; the latter may be null.

Item

public enum Item
{
    SANDWICH("sandwich"), CRISPS("crisps"), DRINK("drink");

private String description;

Item(String description)
{
this.description = description;

}

public String toString()
{
    return description;
}

}

Character

public enum Character
{
    LAURA("Laura",Item.SANDWICH),SALLY("Sally", Item.CRISPS),ANDY("Andy", Item.DRINK),ALEX("Alex", null);


private String Chardescription;
private Item item;

private Character(String Chardescription)
{
    this.Chardescription= Chardescription;
}

private Character(Item item)
{
    this.item= item;
}
public String toString()
{
    return Chardescription;
}

}

The problem is with the enum class Character, When I compile I get the error "No suitable constructor found for Character(java.lang.String.Item)"

Upvotes: 0

Views: 182

Answers (2)

Rapidistul
Rapidistul

Reputation: 564

You have to specify the Item parameter in your constructor.

private Character(String Chardescription, Item item) {
        this.Chardescription = Chardescription;
        this.item = item;
}

Upvotes: 2

rgettman
rgettman

Reputation: 178263

Constructors for any class, not just an Enum, shouldn't be created on a one-per-field basis.

Create one constructor that takes both values at once.

private Character(String Chardescription, Item item) {
    this.Chardescription = Chardescription;
    this.item = item;
}

As an aside, usual Java naming conventions would have you name it charDescription, not Chardescription. Also, it's best not to name any class the same name as a built-in Java class (Character).

Upvotes: 2

Related Questions