• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

What is Enums?

 
Greenhorn
Posts: 14
Mac Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, I have been hearing about enums in java but not sure what it does actually.
Is it a special kind of class or something?
What is the usefulness of Enums?
Where and how I can use Enums?

Getting so much curious
 
Ranch Hand
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we want to represent a group of named constants then we should go for enum.
The main objective of enum is to define our own datatypes (EDTs) i.e Enumerated Datatypes



semicolon after the constants if optional.



Every enum is internally implemented by using class concept.

We can access enum constant by using enum name.

 
Ranch Hand
Posts: 99
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me Google that for you
 
Marshal
Posts: 80761
488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read all about it in the Java™ Tutorials.
 
Greenhorn
Posts: 20
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enums are lists of constants. When you need a predefined list of values which do not represent some kind of numeric or textual data, you should use an enum.
More precisely, an enum type is a special kind of Java class. An enum can contain constants, methods etc.

Find out more about enums -

Java Arrays and Enums

Java Enums

 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found a lovely tutorial here :
http://howtodoinjava.com/2012/12/07/guide-for-understanding-enum-in-java/
It clarified a few things for me as well

Collecting key notes about enum

  • enums are implicitly final subclasses of java.lang.Enum
  • if an enum is a member of a class, it’s implicitly static
  • new can never be used with an enum, even within the enum type itself
  • name and valueOf simply use the text of the enum constants, while toString may be overridden to provide any content, if desired
  • for enum constants, equals and == amount to the same thing, and can be used interchangeably
  • enum constants are implicitly public static final
  • the order of appearance of enum constants is called their “natural order”, and defines the order used by other items as well : compareTo, iteration order of values , EnumSet, EnumSet.range.
  • Constructors for an enum type should be declared as private. The compiler allows non private declares for constructors, but this seems misleading to the reader, since new can never be used with enum types.
  • Since these enumeration instances are all effectively singletons, they can be compared for equality using identity (“==”).
  • you can use Enum in Java inside Switch statement like int or char primitive data type
  •  
    Campbell Ritchie
    Marshal
    Posts: 80761
    488
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I still think the Java™ Tutorials link I gave you yesterday is the best enum tutorial to date. You will see at least two of the tutorials says, “more powerful”. That is something important to remember. Because an enum constant is a full‑blown object/instance of its class, with methods and all. The Java Tutorials link shows you how to calculate weights on different planets with an enum.
     
    Sankalp Bhagat
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I still think the Java™ Tutorials link I gave you yesterday is the best enum tutorial to date. You will see at least two of the tutorials says, “more powerful”. That is something important to remember. Because an enum constant is a full‑blown object/instance of its class, with methods and all. The Java Tutorials link shows you how to calculate weights on different planets with an enum.



    I don't think so Mr. Campbell Ritchie.

    There's nothing given in your link about enum.

    we can't instantiate enum types. That's not given.

    enum can neither extend any other enum or class
    nor any other enum or class can extend our enum. The reason is not given. Only it says java language does not support multiple inheritance that's why....

    but any enum can implement any number of interface. This is not given.

    Every enum is implicitly a final class. This is not given.

    Where semicolon (;) should be optional and where it should be mandatory. This is not given.

    Every constant is implicitly written as public static final. This is not given.

    We can declare enum only within the class or outside the class. But not inside a method. This is not given.

    coderanch, <default>, strictfp, private, protected, static are the only applicable modifiers for enum declared inside the class. This is not given.

    coderanch, <default>, strictfp are the only applicable modifiers for enum declared outside the class. This is not given.


    enum types must not be local. This is not given.

    we can pass enum type as an argument to switch statement. This is also not given.

    But every case label should be a valid enum constant. This is not even given.

    Every enum in java implicitly extends java.lang.Enum class,
    then why we get compile time error if we explicitly extends enum to java.lang.Enum ? This is not given in your link too.

    then what is given in your link you think is the best tutorial to date?

    In every post you reply and what makes you think yours answer is the best of all?
     
    Campbell Ritchie
    Marshal
    Posts: 80761
    488
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You would appear not to have read the link correctly.
     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sankalp you need to cool down a bit. Campbell Ritchie is here since years helping out folks voluntarily. Infact, we are all volunteers here. We go out of our current work tasks and find out stuff to help out people in coding.

    You have pointed out a lot of stuff that you feel is missing out in the tutorial link and you have the right to ask:

    Sankalp Bhagat wrote:then what is given in your link you think is the best tutorial to date?



    But, I find a line in our post very rude and personal:

    Sankalp Bhagat wrote:In every post you reply and what makes you think yours answer is the best of all?



     
    Sankalp Bhagat
    Ranch Hand
    Posts: 64
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:Sankalp you need to cool down a bit. Campbell Ritchie is here since years helping out folks voluntarily. Infact, we are all volunteers here. We go out of our current work tasks and find out stuff to help out people in coding.

    You have pointed out a lot of stuff that you feel is missing out in the tutorial link and you have the right to ask:

    Sankalp Bhagat wrote:then what is given in your link you think is the best tutorial to date?



    But, I find a line in our post very rude and personal:

    Sankalp Bhagat wrote:In every post you reply and what makes you think yours answer is the best of all?





    I ask forgiveness for that. I shouldn't have said that. of course Cambell Ritchie is an expert and I'm a beginner. No way I can judge his knowledge. I'm sorry once again Mr. Campbell Ritchie. Hope you would forgive me.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic