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 / General / February 2006

Tip: Looking for answers? Try searching our database.

wh do I get an error when I tried to imoirt enum constants.

Thread view: 
ab2305@gmail.com - 05 Feb 2006 00:53 GMT
package myenum;

import myenum.MyEnum.*
import java.io.*;

class MyClass{

public MyClass(){ System.out.println(A);}

}

enum MyEnum{

A, B, C
}

I get an compilation error. Also when I put enum into a different file,
it didnt work. I thought in 1,5   you cn import static members...

please explain
Roedy Green - 05 Feb 2006 01:48 GMT
>import myenum.MyEnum.*

The enum constants are just objects or sometimes are inner classes of
MyEnum. When you import MyEnum they come along with it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Hawtin - 05 Feb 2006 12:18 GMT
>> import myenum.MyEnum.*
>
> The enum constants are just objects or sometimes are inner classes of
> MyEnum. When you import MyEnum they come along with it.

Enum constants are not imported along with the enum.

Within case labels the enum type is determined by the switch expression,
and used to qualify the label identifiers. You don't even need to import
the enum for that.

Import works the same for enums as for other types.

The correct syntax for static imports is:

import static myenum.MyEnum.*;

or

import static myenum.MyEnum.A;
import static myenum.MyEnum.B;
import static myenum.MyEnum.C;

The alternative is to qualify the constant with the enum type name:

import myenum.MyEnum;
...
    MyEnum mine = MyEnum.A;

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

James Westby - 05 Feb 2006 03:56 GMT
> package myenum;
>
> import myenum.MyEnum.*

Should it be import myenum.MyEnum ??? A .* impiles importing all members
of a package, but MyEnum is not a package.

> import java.io.*;
>
[quoted text clipped - 13 lines]
>
> please explain

James


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.