views:

100

answers:

5

Recently i came across static import feature available in java . But was not so happy as this is available only for 1.5 or above. We work on jdk 1.3 and constantly implement interface just for the ease of accessing the constants. But i feel this is a wrong way of utilizing inheritence. Is there any alternative for this? other than of course specifying the class/ interface name . Or is it possible to make use of static import in jdk 1.3 with some tweak??Please note that we use jdk 1.3, eclipse 3.6 and windows xp for our project developement.

A: 

Staying on such an old version of the JDK (it's been dead for almost 5 years now, and that's after the 4 year EOL period!) comes with a price -- you can't use the newer features in newer JVMs. You might be able to hack something together using preprocessing, or some special build process - but don't. Bite the bullet and upgrade.

Steven Schlansker
'Bitting the bullet' might not be an option at all. Embedded systems are a different problem than desktop or server systems. For the OP it might be plain impossible to 'upgrade'.
Grodriguez
This answer in no way answers the OP's question.
Chris Knight
If I complained that DOS doesn't support IPV6, you'd tell me I'm silly for expecting such a thing. How is this any different?
Steven Schlansker
Also @Chris Knight / Grodriguez: when I answered this, he had not yet mentioned that it was for an embedded project, and it still sounded like that he was targeting XP...
Steven Schlansker
The question itself states the problem is because the static import feature is available post 1.5./ If upgrading was possible ths post wud not have come. I think it was obvious thing
Ravisha
+2  A: 

use interface for that purpose is not wrong. it's not a blasphemy on a heavenly concept. make do with what you have, don't get religious.

actually i don't think "static import" is used a lot. it's creepy. i would rather prefix names with originating class names. omitting package names - that I can handle. ommiting class names - very confusing.

irreputable
+1  A: 

Not a solution but it's better to use a final class with public static final variables for constants rather than an interface. Just my two cents.

John Engelman
I definitely agree to what you say john.
Ravisha
A: 

I believe that using interfaces to group public constants is actually cleaner and easier to read than using static imports. In any case, I would not call it a 'wrong way of utilizing inheritance'.

And yes, there are situations where you simply cannot upgrade to the latest JDK available. This happens often with embedded systems.

Grodriguez
Ravisha is talking about having classes implement the interfaces to use the constants without prefixing them with the interface name... which is definitely a wrong way of using interfaces.
Michael Borgwardt
It is a matter of style, and as such there can be no absolute rights or wrongs. I personally prefer prefixing all accesses to interface constants with the interface name (which is why I don't like static imports either), but that's just my preference. I would not go so far as to say that this is 'right' and the alternative is 'wrong'.
Grodriguez
Not prefixing interfaces (i.e. implementing them) *is* definitely wrong and because you can't prevent people from doing this, using interfaces for constants is considered as an anti-pattern. This is *Item 17 - Constant Interface Antipattern* of Effective Java.
Pascal Thivent
As I said earlier, I myself agree with this style. I just don't agree with the idea that styles should be "enforced".
Grodriguez
+2  A: 

other than of course specifying the class/ interface name

What's so horrible about that anyway? It's the correct way of doing it. Much better than static imports, if you choose the names of the classes and the constants well (no need to use interfaces, put the constants where they conceptually belong).

Michael Borgwardt