Results 1 to 12 of 12
  1. #1
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Question passing an enum type as a parameter ??!

    hello all.. I'm just wondering if its possible to pass an enum type as a parameter ??

    this is my program. The last 2 lines in the constructor are not workin.. how can I pass the 2 enum types as parameters and assign them to the original enum values above ??

    public class Person {

    enum Sex { MALE, FEMALE }
    enum Size {SMALL, MEDIUM, LARGE, X LARGE}

    String name;
    int age;
    double money,

    public Person (String name, int age, double money, Sex sex, Size size) {
    this.name = name;
    this.age = age;
    this.money = money;

    this.Sex = sex;
    this.Size = size;
    }


    }

    Thanks for the help.
    Last edited by SCS17; 07-11-2008 at 07:01 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    enums are immutable in Java. So it's expecting that pass to enum by reference. But Java only allowed to pass-by-value. Just think about it first.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    What is the error message you are getting? It really helps us if you'd give that!!!

    Is it like this:
    Person.java:17: cannot find symbol
    symbol : variable sex
    location: class TestEnum
    this.sex = sex;

    Where in your program is the variable sex defined?
    Add a definition for it and the compiler should be happy.

  4. #4
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What is the error message you are getting? It really helps us if you'd give that!!!

    Is it like this:
    Person.java:17: cannot find symbol
    symbol : variable sex
    location: class TestEnum
    this.sex = sex;

    Where in your program is the variable sex defined?
    Add a definition for it and the compiler should be happy.
    The error is "cant find symbol variable Sex" & "cant find symbol variable Size".

    I'm referring to the enum Sex and enum Size as an instance variable with the (this) in the constructor. And I'm takin them as parameters as variables too.. (Sex sex) & (Size size).. and thats my question..is it possible to do that somehow or not ??

    Thanks for the help.

  5. #5
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    enums are immutable in Java. So it's expecting that pass to enum by reference. But Java only allowed to pass-by-value. Just think about it first.
    I see what you mean.. so Java treats enums like objects, and their value wont get changed even if it was possible to pass it as an argument in the constructor. It makes kind of sense now..

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    cant find symbol variable Sex" & "cant find symbol variable Size".
    Is that the exact text of the error message?
    Or did the error message have sex and size? Note lowercase!!!
    The name of the variable, not the name of the enum. Look at the error message I posted.
    I was able to get the program to compile by adding the correct definitions and able to create a new instance of the class.

  7. #7
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Is that the exact text of the error message?
    Or did the error message have sex and size? Note lowercase!!!
    The name of the variable, not the name of the enum. Look at the error message I posted.
    I was able to get the program to compile by adding the correct definitions and able to create a new instance of the class.
    Yes thats the exact message. Like I mentioned in my previous post, I think the compiler is confused because I referring to the enum type with the keyword (this.) so the compiler is assuming that there exists an instance variable called Size and Sex. Anyway.. I tried a couple of other things and It didnt work out either.

    Like Eranka mentioend.. I think theres no way that you can take an enum as a parameter and then assign values to it, because enums are immutable.

    Anyway, thanks for the help guys.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Please compile your program and copy the full text of the error messages here. What you posted looks like you editted the output.
    It's very important the we see the EXACT error message as produced by the compiler!

    As I said before, I was able to make some very simple additions/changes to your code and have it compile OK. Your code will work. Keep trying.

    Clue: where is the variable referenced by "this.Sex" defined? Sex is the name of an enum, not the name of a variable. You need a variable of type Sex (perhaps called sex) to save the passed variables in.
    Last edited by Norm; 07-12-2008 at 04:24 PM.

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post yes

    Quote Originally Posted by SCS17 View Post
    hello all.. I'm just wondering if its possible to pass an enum type as a parameter ??
    http://java.sun.com/j2se/1.5.0/docs/...ge/enums.htmlv
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  10. #10
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Treat Enums as a special kind of class, where the enum elements are public static instances automatically created for you:
    Java Code:
    enum Sex { MALE, FEMALE }
    enum Size { SMALL, MEDIUM, LARGE, X_LARGE }
    
    class Person {
        Sex sex;
        Size size;
        String name;
        int age;
        double money;
    
        public Person(String name, int age, double money, Sex sex, Size size) {
            this.name = name;
            this.age = age;
            this.money = money;
    
            this.sex = sex;
            this.size = size;
        }
    }

  11. #11
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Clue: where is the variable referenced by "this.Sex" defined? Sex is the name of an enum, not the name of a variable. You need a variable of type Sex (perhaps called sex) to save the passed variables in.
    Thats what I've been saying.. I've been referring to the enum type named Sex as an instance variable in my constructor with (this.Sex).
    Anyway I tried making a Sex and Size objects inside the class, which is exactly what you're suggesting and it worked !!.

    Now of course they'll be treated as static methods:

    Person person = new Person("Name", 21, 2.20, Person.Sex.MALE, Person.Size.MEDIUM);

    But it makes more sense now.

  12. #12
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by dlorde View Post
    Treat Enums as a special kind of class, where the enum elements are public static instances automatically created for you:
    Java Code:
    enum Sex { MALE, FEMALE }
    enum Size { SMALL, MEDIUM, LARGE, X_LARGE }
    
    class Person {
        Sex sex;
        Size size;
        String name;
        int age;
        double money;
    
        public Person(String name, int age, double money, Sex sex, Size size) {
            this.name = name;
            this.age = age;
            this.money = money;
    
            this.sex = sex;
            this.size = size;
        }
    }
    Exactly what I did. Thanks :).

Similar Threads

  1. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  2. Passing short value as parameter
    By javanewbie83 in forum New To Java
    Replies: 16
    Last Post: 07-16-2008, 05:27 AM
  3. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 06:24 PM
  4. Replies: 1
    Last Post: 05-08-2008, 09:38 AM
  5. arugment/parameter
    By ravian in forum New To Java
    Replies: 5
    Last Post: 01-04-2008, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •