This week's book giveaway is in the Spring forum.
We're giving away four copies of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 and have Marten Deinum & Iuliana Cosmina on-line!
See this thread for details.
Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

enums in Eclipse

 
Ranch Hand
Posts: 59
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to make a simple enum of several intevers....  Everything I try doesn't work.  Can someone walk me through the steps?  For example, say I want an enum  MyValidValues {1, 2, 3, 4, 9}...  If it matters, I want it to be usable in my package, bitPlayer, only.

Thanks in advance for your help.  ~d
 
Saloon Keeper
Posts: 8228
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Example:
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }

You'll need to be a little clearer in your question as to what it is you hope to do with enums. Perhaps post an example of your attempt.
 
Master Rancher
Posts: 3889
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An enum isn't really for integer values.  You could associate each enum value with an integer, if you want.  See the "Planet" enum in the link Carey gave you, for an example.  (Those use double rather than int, but a similar idea.). Alternately, do you even need an enum?  Perhaps a Set<Integer> can contain all the valid values?
 
Marshal
Posts: 72944
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which language is that supposed to be in? Java® enums are collections (fixed arity) of instances of their own class; they are not simply integers like in C/C++. You can't write an enum constant called after a number. What you can do is to writeHave a look in the Java™ Tutorials and the JLS (=Java® Language Specification). It may appear strange, but there are several tutorial‑like examples in the JLS, which you wlll find helpful.

[Addition} To make something accessible only in your package give it package‑private access (=default access), which you do by not writing any access modifiers.
 
David Cone
Ranch Hand
Posts: 59
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enums are not the issue.  The issue, is getting them to work in Eclipse (hence my Title).  Eclipse wants enums in a seperate file.  I don't understand "Encapsulating type", I can find a list of valid types...   No matter what I try, it doesn't work.  I know I can't be the first one to try and do it in Eclipse, someone smarter (or at least more experienced) can surely solve this.  I know I can use a different editor like JEdit, but I like many features of Eclipse, and I'd like to stick with it, if I can.
 
David Cone
Ranch Hand
Posts: 59
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Which language is that supposed to be in? Java®[


Yes, Java.

If Java treats them as strings could I use  another functio9n to convert them?   I want to use a For Each loop....  Is it better to put them in a Final int array?

 
Mike Simmons
Master Rancher
Posts: 3889
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, code like "enum  MyValidValues {1, 2, 3, 4, 9}" won't work in any Java environment - it's not an eclipse thing; it's that you don't have a valid enum.  Each enum value needs a name, and a number is not a legal name in Java. So the notion of an "enum of integers" is, at best, not very clear, and that's why Carey is pointing you to a basic enum tutorial, and I'm questioning if you need an enum at all.  Can you explain a bit more about what you need this for, and why you think you need an enum?
 
David Cone
Ranch Hand
Posts: 59
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:But, code like "enum  MyValidValues {1, 2, 3, 4, 9}" won't work in any Java environment - it's not an eclipse thing; it's that you don't have a valid enum.  Each enum value needs a name, and a number is not a legal name in Java. So the notion of an "enum of integers" is, at best, not very clear, and that's why Carey is pointing you to a basic enum tutorial, and I'm questioning if you need an enum at all.  Can you explain a bit more about what you need this for, and why you think you need an enum?



The main feature of enum I liked is it's mot changeable.  The second feature I was interested in was using a For/Each loop to go through the list.  BTW, I read that enum tutorial (and several others) none of them made it clear that it couldn't be numbers.  They used text as an example, but didn't specify what were or were not valid enum entries.  Thank you for that clarification! ~d
 
Carey Brown
Saloon Keeper
Posts: 8228
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

 
David Cone
Ranch Hand
Posts: 59
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.



I never considered "constants" as being textual only.  Pi is a constant (to me).  I admit it's been many years since I did PHP programming, but I thought you could define numerical constants there.  My appologies for the confusion.

~d
 
Carey Brown
Saloon Keeper
Posts: 8228
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, yes. In Java you can have constants such as 3.14 but when the word "constant" is used it is generally taken to mean defined constant. So you can define 3.14 as the constant PI like this:
So now we consider the word "PI" to be the defined constant, or just "constant".
 
Campbell Ritchie
Marshal
Posts: 72944
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Cone wrote:. . .
If Java treats them as strings could I use  another functio9n to convert them?

It doesn't. No, you can't. Both links we have quoted show how the arguments in (...) are used. Remember I said the code I posted was incomplete.

 I want to use a For Each loop....  Is it better to put them in a Final int array?

You have said how you want to use them and not said what you want to do; that is sort of putting the cart before the horse. We can only help if we know what you want to do. No, I don't think a final array will help.
The tutorial (we all seem to have posted links to the same tutorial) doesn't treat enum constants as text, but as code. The two are different.

. . . that enum tutorial (and several others) none of them made it clear that it couldn't be numbers. . . .

Maybe this tutorial does. It is an old version of the tutorial we linked to and it says that enums in other languages are simply glorified integers. Maybe there were comnplaints about that bit, because it was withdrawn. No, it doesn't actually say that. You will have to look at the grammar, which is in the JLS link I quoted; §8.9.1 shows an enum constant as containing an identifier, and identifiers don't start with a number.
Yes, you can get all the constants out of an enum with its values() method (see JLS section) and you can iterate that array. But enums aren't really intended to be used instead of arrays. Maybe you want an immutable List, which you can get like this:-We can only work out what you ought to do it you tell us what you are trying to do.

Please show us the code where you seem to have the enum as a nested type (inside another type).
 
Saloon Keeper
Posts: 23696
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Cone wrote:Enums are not the issue.  The issue, is getting them to work in Eclipse (hence my Title).



No it isn't. Eclipse is neither a programming language nor is it the application you want to run. It's an IDE.

David Cone wrote:Eclipse wants enums in a seperate file.



No it doesn't. Again, Eclipse is just the development environment that you happen to be using. "Wanting" things is determined by the Java language specification, not the IDE. And no, Java does not demand that enums always live in their own file. They are often declared in the same way that inner classes are declared.
 
David Cone
Ranch Hand
Posts: 59
Eclipse IDE MySQL Database Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Ah, yes. In Java you can have constants such as 3.14 but when the word "constant" is used it is generally taken to mean defined constant. So you can define 3.14 as the constant PI like this:
So now we consider the word "PI" to be the defined constant, or just "constant".



Got it...  I found a non-enum was to accoplish what I was trying to do.  I thank you (and the rest) for your help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic