• 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

Importing enums

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a few doubts regarding the static members,enums and importing them from packages.

Let's say we have a following enum declaration :


My first question is whether the 'Result' is "public static final" ?
If I want to use above enum declaration in another class, what import statement do I have to use if I want to use enum constants as
1. 'Result.CORRECT'
2. 'Result result=CORRECT'
3.

Please explain.

Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether Result is 'public static final': What do you mean exactly? It's public, because you specified that yourself in your code. It's not static, because top-level classes (and enums) cannot be static. You can regard it as being final, because you cannot extend an enum (you cannot create a subclass using an enum as the superclass).

1. import mypkg.Result;
2. import static mypkg.Result.*;

More about 'import static'.

What is your question about point 3?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find help about your question 3 in the Java Language Specification.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ritchie and Jesper for a quick reply.


Whether Result is 'public static final': What do you mean exactly? It's public, because you specified that yourself in your code. It's not static, because top-level classes (and enums) cannot be static. You can regard it as being final, because you cannot extend an enum (you cannot create a subclass using an enum as the superclass).



By static I meant , if I could use an enum constant like 'CORRECT' without creating an enum reference like:


Also can you please explain me the use of constructors, members and 'constant-specific class bodies' inside enum declarations. I always thought, coming from a C/C++ background, that enumerations are a mean to define named constants. So whats the concept behind the implementation and use of constructors, members and 'constant-specific class bodies' inside enum declarations?
Aren't plain old classes with nested enum declarations better for the purposes, because having all those constructors etc. in a enum declarations make them seem like classes to me anyway.
Maybe its a very simple concept that I'm missing about Enums in Java, I only started learning it a week back.

Thanks
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aren't plain old classes with nested enum declarations better for the purposes, because having all those constructors etc. in a enum declarations make them seem like classes to me anyway.
Maybe its a very simple concept that I'm missing about Enums in Java, I only started learning it a week back.



Deepak, in Java 1.5 or later, enums are classes, and the older int constant style is deprecated.

John.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Borania wrote:By static I meant , if I could use an enum constant like 'CORRECT' without creating an enum reference like:


The enum constants are static. You can't create instances of an enum outside the enum itself; the line of code above does not compile.

Enums in Java are real, type-safe enums, not half-baked named constants as they are in C or C++.

Aren't plain old classes with nested enum declarations better for the purposes, because having all those constructors etc. in a enum declarations make them seem like classes to me anyway.


You can regard an enum in Java as a special kind of class, but having language support for them is better than having to simulate them with other language features; this makes your code more clear and type-safe.

For more info about enums in Java, see: Enums and Enum Types.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks man

Will look at the links you gave.should help.These enums in java and importing them are driving me crazy.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Borania wrote:I always thought, coming from a C/C++ background . . .

It is very easy to think that Java™ is an extension of C/C++. I think the fact that so much Java syntax is copied from C/C++ makes it easier to think that.

But Java is not an extension or a refinement of C/C++. It is a language of its own. Syntactic constructs which look the same might mean something totally different. The French word sympatique, the Italian word simpatico and the German word sympatisch mean more-or-less the same thing (at least, I think they do). But the English word sympathetic means something different. Similarly Java enums are different from C/C++ enums.
Other Java constructs which are different from C/C++ include static and protected.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interestingly put.

I haven't reached inheritance as yet, but thanks for the heads-up on protected.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Believe me you will love inheritance and other parts of java. I also started with c/c++ but java is in a level of its own. I just love threads and the way threads can be used in java. In c we have to have win32thread.h and then specific functions that are thread specific. Also network programming in java is much more fun than in C or C++.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah !!
Network programming was the actual reason I started learning java.
 
Shiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic