• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enum Constants

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning everyone,
I have a question for you.

What is the name of  the members declared in the Enum?
My teacher called them "constants".
From my point of view, it is not so appropriate, because those ones are objects rather than constants.
I give you an example:



What do you think about my thought? How would you define with an appropriate name MERCURY, VENUS, EARTH, etc. ?
 
Bartender
Posts: 390
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are constant values, once they are created they cannot be changed

From Java Tutorials — Enum Types:

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.

Later on:

Each enum constant is declared with values for the mass and radius parameters. These values are passed to the constructor when the constant is created. Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon.


Your teacher is correct, they are referred to as enum constants. It does make sense, because enums are the datatype made to represent a fixed set of constants.
I hope that helps  

Edit: note that your example of an enum is an example on the page I linked to; I think it probably comes from there.
 
Saloon Keeper
Posts: 27762
196
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
It is possible for complex objects to be constant objects (note: constant, not "costant"). Also known as immutable objects. Both words mean the same thing: the item in question cannot be changed.

It is true that usually an enumeration class declaration defines a set of primitive, usually integer values, but Java enums expand on that concept quite a bit.

Rejoice. Back when COBOL was in its prime, programming languages generally did not have a way to indicate a manifest constant value to the compiler. One had to simply remember not to modify "constants". Forgetting to do so led to the maxim "constants - aren't and variables - won't".
 
Claud Mann
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:It is possible for complex objects to be constant objects (note: constant, not "costant"). Also known as immutable objects. Both words mean the same thing: the item in question cannot be changed.

It is true that usually an enumeration class declaration defines a set of primitive, usually integer values, but Java enums expand on that concept quite a bit.

Rejoice. Back when COBOL was in its prime, programming languages generally did not have a way to indicate a manifest constant value to the compiler. One had to simply remember not to modify "constants". Forgetting to do so led to the maxim "constants - aren't and variables - won't".



thank you guys!! Now it's clear.
p.s. thanks also for the grammar correction, I made a bad mistake (:
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However... it is possible to create mutable enum "constants". For example you could add this method to the example:



provided you removed "final" from the declaration of radius.

And yes, that could certainly be "considered harmful", but I suppose there could be use cases for it.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can envisage a Fuel enum where the different constants include price as a field, but I don't think I dare write it.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the term "enum constant", the "constant" refers only to the fact that the fields for the enum values are implicitly final.  Planet.MERCURY will always refer to the same instance of the Planet enum, even if you let the mass and radius be mutable.  An enum constant can however refer to a mutable instance.  Even if we almost always implement enums as immutable - it's not enforced by the language.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic