Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2007

Tip: Looking for answers? Try searching our database.

Generics and Enums

Thread view: 
amberarrow@gmail.com - 29 Jul 2007 18:55 GMT
Hi,

I'm trying to write a generic class that takes an Enum as generic
parameter but
I'm not able to compile it:
------------------------------------
public class Foo<T extends Enum> {
   public Foo() {
       for ( T t : T.values() ) {
           System.out.println( "t = " + t );
       }
   }
   enum Stuff {RED, BLUE, GREEN};
   public static void main( String args[] ) throws Exception {
       Foo<Stuff> foo = new Foo<Stuff>();
   }
}
--------------------------------------------
I get a compile error:
Foo.java:3: cannot find symbol
symbol  : method values()
location: class java.lang.Enum
       for ( T t : T.values() ) {
                    ^
Roedy Green - 29 Jul 2007 19:32 GMT
On Sun, 29 Jul 2007 10:55:27 -0700, "amberarrow@gmail.com"
<amberarrow@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>I'm trying to write a generic class that takes an Enum as generic
>parameter but
>I'm not able to compile it:
>------------------------------------
>public class Foo<T extends Enum> {

Your class is similar to an EnumSet in terms of its Generics.  So I
had a peek at how EnumSet does this

public abstract class EnumSet<E extends Enum<E>> extends
AbstractSet<E>
   implements Cloneable, java.io.Serializable
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Lew - 29 Jul 2007 21:54 GMT
> On Sun, 29 Jul 2007 10:55:27 -0700, "amberarrow@gmail.com"
> <amberarrow@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 12 lines]
> AbstractSet<E>
>     implements Cloneable, java.io.Serializable

This would allow the OP to eliminate the illegal "T.values()" expression and
just call "values()".

Signature

Lew

amberarrow@gmail.com - 29 Jul 2007 23:12 GMT
> > On Sun, 29 Jul 2007 10:55:27 -0700, "amberar...@gmail.com"
> > <amberar...@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 18 lines]
> --
> Lew

I changed "Foo<T extends Enum>" to "Foo<T extends Enum<T>>" but get
the same error.
I also changed T.values() to just values() but that results in a
compile error
saying it cannot find the method values().
Patricia Shanahan - 30 Jul 2007 01:28 GMT
>>> On Sun, 29 Jul 2007 10:55:27 -0700, "amberar...@gmail.com"
>>> <amberar...@gmail.com> wrote, quoted or indirectly quoted someone who
[quoted text clipped - 20 lines]
> compile error
> saying it cannot find the method values().

I think you need to copy one more thing from EnumSet. Each of its
methods gets as parameter an element of the enum, or the Enum's Class
object, or an existing collection based on the Enum.

For example, noneOf requires the Class object.

In effect, it always has access to the Class object, either directly or
by asking an element for its getClass(). As it happens, Class has a
method getEnumConstants().

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.