| Author |
Importing enums
|
Deepak Borania
Ranch Hand
Joined: Jul 28, 2009
Posts: 45
|
|
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
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
|
You might find help about your question 3 in the Java Language Specification.
|
 |
Deepak Borania
Ranch Hand
Joined: Jul 28, 2009
Posts: 45
|
|
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
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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
Joined: Jul 28, 2009
Posts: 45
|
|
Thanks man
Will look at the links you gave.should help.These enums in java and importing them are driving me crazy.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
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
Joined: Jul 28, 2009
Posts: 45
|
|
Interestingly put.
I haven't reached inheritance as yet, but thanks for the heads-up on protected.
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
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++.
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
Deepak Borania
Ranch Hand
Joined: Jul 28, 2009
Posts: 45
|
|
yeah !!
Network programming was the actual reason I started learning java.
|
 |
 |
|
|
subject: Importing enums
|
|
|