[Solved]Trouble using enum in constructor when creating a class
I am having trouble getting my enum to work when I try to create a new class and construct it.
Code :
public enum MonsterType{
FIRE,
WATER,
EARTH,
AIR
}
This is my enum values for now. I am creating a monster that will have various attributes including its elemental type.
When I try to construct my monster like this:
Code :
Monster mymonster = new Monster("Goofy", 20, 10, 4, 3, 1, MonsterType.FIRE);
Netbeans says "cannot find symbol".
My constructor looks like this:
Code :
public Monster(String monsterName, int monsterHealth, int monsterAttack, int monsterDefense,
int monsterSpecialAttack, int monsterSpecialDefense, MonsterType monsterType ){
name = monsterName;
health = monsterHealth;
attack = monsterAttack;
defense = monsterDefense;
specialAttack = monsterSpecialAttack;
specialDefense = monsterSpecialDefense;
type = monsterType;
xp = 1000;
So a few questions I have are:
Can I send the enum value like this or is my syntax wrong?
Also if I am using my main.java to make new monsters, can I just declare the enum MonsterType in the main.java class or does it also have to be in Monster class where the constructor is?
Re: Trouble using enum in constructor when creating a class
Quote:
Originally Posted by
willmer
I am having trouble getting my enum to work when I try to create a new class and construct it.
If you want help, you'll have to post an SSCCE. We can't really tell you what's going on without seeing your actual code, and we don't have time to wade through anything extra. Make a separate example program that shows the same error.
Quote:
Originally Posted by
willmer
Can I send the enum value like this or is my syntax wrong?
You can.
Quote:
Originally Posted by
willmer
Also if I am using my main.java to make new monsters, can I just declare the enum MonsterType in the main.java class or does it also have to be in Monster class where the constructor is?
I'm not sure what you mean by "main.java", but I'm guessing your MonsterType is in your Monster class? If so, you're going to want to use the containing class name to access it. Declaring it twice will make two different enums that have nothing to do with one another other than they happen to have the same names.
Re: Trouble using enum in constructor when creating a class
I suppose I should read the stickies huh?
Instead of trying to fix my code. Could you tell me the proper way to create an enum and then use it when creating an object?
like would this be correct?
Code :
enum EnumName{
//various values
}
class className = new class ( EnumName value);
Re: Trouble using enum in constructor when creating a class
No, that's not going to be correct syntax, and the compiler probably told you that. I'm tired of guessing. If you want further help, post the SSCCE. But your problem is probably that you're trying to access an enum defined in one class from another, and the solution is probably to call it using the containing class name first, something like Monster.MonsterType.FIRE. If that doesn't help you, post an SSCCE.