Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trouble using enum in constructor when creating a class

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [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.

    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:
    Monster mymonster = new Monster("Goofy", 20, 10, 4, 3, 1, MonsterType.FIRE);
    Netbeans says "cannot find symbol".

    My constructor looks like this:
     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?
    Last edited by willmer; July 13th, 2011 at 10:36 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble using enum in constructor when creating a class

    Quote Originally Posted by willmer View Post
    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 View Post
    Can I send the enum value like this or is my syntax wrong?
    You can.

    Quote Originally Posted by willmer View Post
    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.
    Last edited by KevinWorkman; July 13th, 2011 at 10:49 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    enum EnumName{
        //various values
    }
     
    class className = new class ( EnumName value);

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  2. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM
  3. Construct a class that implement ActionListener with no constructor
    By striko_514 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2010, 03:15 PM
  4. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM