This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

enum in Tiger  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are my note about enums in Tiger:
Thanks to "Java 1.5 Tiger: A Developer Notebook"

1- Simple definition

enum E {V1, V2, ..., Vn} n >= 0

It's a special class that contains public static final instances of itself.

2- E above implicitly extends java.lang.Enum.
The later is not an enum, and can't be extended explicitly with
class E extends java.lang.Enum {}

3- E constructors are implicitly private, but can be explicit

4- E is effectively final but can't be declared final nor abstract!

5- The values are static references so they can be compared with ==

6- E implments java.lang.Comparable (Vi < V(i+1))

7- E implicitly override toString(): Vi.toString = "Vi", but you can specify another override.

8- E contains: public static E valueOf(String) which (should) complements toString

9- E contains: public int ordinal() which gives the value order (0-based)

10- E contains: public static E[] values()

11- Inner (member) Enums are implicitly static, but can be declared explicitly.

12- You can switch on an enum veriable:
E v = E.V1;
switch(v) {
case V1:
case V2:
default:
}

you can write: case E.V1

If (the enum and the switch are in the same compilation unit)
a jump table is created and indexed by v.ordinal();
else
the switch is converted to an if-then-else;

13- EnumMap<? extends Enum, String> maps enums to string efficiently
created with
EnumMap<E, String> m = new EnumMap<E, String>(E.class);

14- EnumSet contains:
public static EnumSet allOf(Class elementType);
public static EnumSet complmentOf(EnumSet e);
public static EnumSet copyOf(Collection c);
public static EnumSet of(E... e);
public static EnumSet range(E from, E to);

15- Enums can contain constructors, methods and veriables
enum E {
V1(..), V2(..), ..., Vn(..);
E(..){};
variables;
methods;
}

16- Enums can implment interfaces

17- Enums cannot be local classes (defined within a method)

18- Each enum value can override the enum with an ananymous class
enum E {
V1(..) {
f(){}
},
V2 {
f(){}
};

E(..){}
E() {}
f(){}
}

But as in the visitor pattern, it's not apporved:

enum E {
V1(..),
V2;
E(..){}
E() {}
f(){
switch (this) {
case V1:
case V2:
}
}
}
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bilal,

Thanks for sharing your notes with us.

12- You can switch on an enum veriable:
E v = E.V1;
switch(v) {
case V1:
case V2:
default:
}

you can write: case E.V1

By qualifying an enumeration constant in the case statement, i.e. E.V1, will result in compilation error.



Joyce
 
Bilal Al-Sallakh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I exactly meant that but wrote you can instead of you can't!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!