Results 1 to 12 of 12
- 07-11-2008, 08:32 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
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.
- 07-11-2008, 11:59 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 07-11-2008, 04:19 PM #3
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.
- 07-11-2008, 07:05 PM #4
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
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.
- 07-11-2008, 07:11 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
- 07-11-2008, 08:31 PM #6
Is that the exact text of the error message?cant find symbol variable Sex" & "cant find symbol variable Size".
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.
- 07-12-2008, 01:08 PM #7
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
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.
- 07-12-2008, 04:21 PM #8
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.
- 07-12-2008, 06:13 PM #9
yes
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-13-2008, 01:57 AM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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; } }
- 07-13-2008, 01:43 PM #11
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
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.
- 07-13-2008, 01:44 PM #12
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
Passing short value as parameter
By javanewbie83 in forum New To JavaReplies: 16Last Post: 07-16-2008, 05:27 AM -
Input parameter of Main method
By Java Tip in forum Java TipReplies: 1Last Post: 07-12-2008, 06:24 PM -
Invalid parameter binding! How to debug PreparedStatement.
By rjuyal in forum Advanced JavaReplies: 1Last Post: 05-08-2008, 09:38 AM -
arugment/parameter
By ravian in forum New To JavaReplies: 5Last Post: 01-04-2008, 09:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks