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.
-
4Java 1.3 is quite dated at this point. Is an upgrade to 1.5 or even 1.6 completely out of the question? There are several other benefits to be gained from an upgrade. Generics are worth it alone. – Asaph Sep 21 '10 at 04:29
-
Hi Asaph. There are lot of project dependencies because of which version upgrade is totally ruled out in my case. – Ravisha Sep 21 '10 at 04:35
-
1@Ravisha - That's the kind of attitude that kept IE 6 around for so long. [I believe, dear chap, that upgrading your code would be the most sensible course of action.] – Amy B Sep 21 '10 at 04:50
-
@Ravisha - Dude! JDK 1.3 on XP? I would quit my job in protest. – dsmith Sep 21 '10 at 05:20
-
well the project is developed on xp thats what i meant. This is not a desktop application . Its an embedded system application. Sorry but i wont be able to give more details on this. which is not required too – Ravisha Sep 21 '10 at 05:30
-
Implementing constant interfaces *is* an horrible practice, prefer a constant class over interfaces. See [What is the use of interface constants?](http://stackoverflow.com/questions/2659593/what-is-the-use-of-interface-constants). – Pascal Thivent Sep 21 '10 at 17:34
5 Answers
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.

- 44,725
- 9
- 65
- 93
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).

- 342,105
- 78
- 482
- 720
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.

- 4,119
- 1
- 16
- 12
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.

- 21,501
- 10
- 63
- 107
-
1Ravisha 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 Sep 21 '10 at 07:24
-
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 Sep 21 '10 at 08:46
-
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 Sep 21 '10 at 17:51
-
As I said earlier, I myself agree with this style. I just don't agree with the idea that styles should be "enforced". – Grodriguez Sep 21 '10 at 17:59
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.

- 37,580
- 14
- 81
- 100
-
'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 Sep 21 '10 at 07:12
-
-
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 Sep 21 '10 at 16:47
-
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 Sep 21 '10 at 16:51
-
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 Sep 23 '10 at 03:54