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

A

ab2305

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
 
R

Roedy Green

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.
 
J

James Westby

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.*;

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


James
 
T

Thomas Hawtin

Roedy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top