Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static import of an enum in Java 1.5?

 
Barry Burd
Author
Ranch Hand
Posts: 118
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I define
enum Scale {Celsius, Fahrenheit, Kelvin, Rakine};
in a file all its own, then I don't seem to be able to do
import static Scale.*;
in any of my other files. Isn't Scale just a class, and aren't the values
of Scale just static members of that class?
 
Barry Burd
Author
Ranch Hand
Posts: 118
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out. Case closed.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Burd:
I figured it out. Case closed.

What did you end up doing? Please share.
 
Barry Burd
Author
Ranch Hand
Posts: 118
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I **thought** I had figured it out, but that's only because I was compiling the wrong code. In fact, I still don't know what's wrong. I can do a static import when I put the enum in a separate package and I do both
import util.Scale;
import static util.Scale.*;
but I can't do the import when I put the enum class in the default package. I guess this means that, since the original import declaration isn't meant to grab anything from the default package, the static import can't do that either. That's not good.
 
Barry Burd
Author
Ranch Hand
Posts: 118
5
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem seems to be with static import for the default package in general, not with enums. For instance, the following code does not compile:
public class Stuff {
public static int i = 7;
}
--------------------...and in another file...
import static Stuff.i;
class UseStuff {
public static void main(String args[]) {
System.out.println(i);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic