What's wrong with this line? 3

Posted by Berin Loritsch Tue, 22 Jan 2008 15:25:00 GMT

Here it is, brace yourself:

private final Logger logger = Logger.getInstance(MyClass.class);

Oh man, is that a dead ringer for bad code. After all… Oh, wait a minute—isn’t that how you initialize loggers all the time? Ok, what if I told you that MyClass is an enum? That’s right, in Java 6 you can’t even access the class static member variable for an enum. In Java 5 this is perfectly legal and your code will compile and work. In Java 6, the powers that be have decided that you can’t do this even if you are compiling for Java 5! This is wrong, and a good way to break everyone’s code.

I’m sure you’re going to ask me what I’m doing with setting up a logger in my enum aren’t you? The trick is that we need to access some property information to determine if we can use certain values in our environment. The logger is there just in case something goes wrong, and something always goes wrong the first time. In the future, the values will be populated with information from a database, so it won’t be an enum any more.

I have to ask, Why would accessing the static class property that is a part of every class object cause a compilation error? Perhaps, in Java 6 the class never truly exists. I don’t know. Still, if you have that ability in Java 5, why would you remove it in the next version? Please Sun, act like you have some sense.