• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Enum properties/values/fields  RSS feed

 
Nicholas d'Hondt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have an Enum file with 119 constants and each constant of that type has 20 fields that come with it. All the fields are the same type and named the same (e.g. there are 119 of Object obj, one for each constant), and I want to run the same methods over them. Since the Objects of the same type are named the same for each constant, I just have them named explicitly in get-er methods.

This worked fine when I just put all 20 fields through the constructor and set them as fields under all the constants. But I realized that if I wanted to make an instance of this Enum class, I'd have to enter in all 20 fields when they are all a set of Objects with unique values. So I then put them as fields under their own respective constant to make it easier to create instances of this enum. But now my methods don't work.

A) I don't really understand why they don't work anymore?
B) Is there a way to fix it without putting all the methods under each constant?
C) Thanks in advance, hope it's clear what I'm doing.


Example:

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code doesn't work -- can you post working and as close to real-life code as possible? It makes it much easier to tell what you're trying to do.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to declare MyObject obj as an instance variable and then access it via the getObjVal()
Check out the Planet example here http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
 
Nicholas d'Hondt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:declare MyObject obj as an instance variable


Uh, what does that mean?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas d'Hondt wrote:
Maneesh Godbole wrote:declare MyObject obj as an instance variable

Uh, what does that mean?

Did you check out the Planet example I suggested? In that example, mass and radius are instance variables
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas d'Hondt wrote:
Maneesh Godbole wrote:declare MyObject obj as an instance variable


Uh, what does that mean?


An instance variable is declared outside of any method body (bu inside the class) and is not static. It's called an instance variable because there is one copy per object instance. If it were static, there would be only one copy per class.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!