question about resource and performance costs of static imports

G

gwlucas

I am considering using static imports in my Java code and was
wondering whether there was a performance cost or resource issues
associated with doing so. Can anyone tell me about how Java
implements the "import static" syntax that was introduced in Java
1.5? Is there anything I need to be careful about?

I think I would have implemented this feature as a simple alias, but
that keyword "static" suggests the possibility that the compiler might
actually be doing something more complicated and potentially costly.
For example, I can see the possibility that "static" means it's
importing byte code from the referenced class. I did try comparing the
size of the class files for the same logic coded with and widthout
static imports and the sizes are similar. But I'm not confident about
drawing any conclusions from that. I am especially concerned about
hidden memory or performance costs.

Thanks,

Gary




Background

Much as I admire the Java programming language, one thing I have
always disliked is the need to include the class name in cases where I
am programming math-intensive applications. For example:

double a = Math.cos( angleInDegrees * Math.PI/180);

If you use math methods once or twice this is no big deal. But anyone
who has ever tried to do something non-trivial knows that this can be
a real problem. Reading math code is hard enough without all the extra
clutter. And though I know that this whole discussion can lead to some
rather thorny issues (it's not unrelated to the whole question of
overloading operators... which I'd never advocate), I've always felt
that at least Math should get some special dispensation. Anyway, the
"import static" syntax renders a lot of this discussion moot. Consider
the following code fragment:

import static java.lang.Math.cos;
import static java.lang.Math.PI;

public class StaticShock {

public static void main(String[] args) {
double a;
a = cos(45*PI/180.0); // a = Math.cos(45*Math.PI/180.0);
System.out.println("a="+a);
}
}
 
D

Daniel Dyer

I am considering using static imports in my Java code and was
wondering whether there was a performance cost or resource issues
associated with doing so. Can anyone tell me about how Java
implements the "import static" syntax that was introduced in Java
1.5? Is there anything I need to be careful about?

I think I would have implemented this feature as a simple alias, but
that keyword "static" suggests the possibility that the compiler might
actually be doing something more complicated and potentially costly.
For example, I can see the possibility that "static" means it's
importing byte code from the referenced class. I did try comparing the
size of the class files for the same logic coded with and widthout
static imports and the sizes are similar. But I'm not confident about
drawing any conclusions from that. I am especially concerned about
hidden memory or performance costs.

There is no runtime impact at all. I haven't verified it for myself, but
I would be extremely surprised if the the compiler did not generate
exactly the same output whether you use static imports of not.

Dan.
 
L

Lew

Daniel said:
There is no runtime impact at all. I haven't verified it for myself,
but I would be extremely surprised if the the compiler did not generate
exactly the same output whether you use static imports of not.

It has to. It's required to. It must. Imports, static or otherwise, are
strictly for the compiler.

Me, I prefer to keep the class name in the static references for the most
part. I don't know why so many people are so lazy about such things - I read
a lot of whining about "extra" typing. Try being the maintenance programmer
who has to debug the thing a year later and you will truly appreciate leaving
the class name in the expression.

Sometimes you have to think of others when you're coding.
 
R

Robert Klemme

It has to. It's required to. It must. Imports, static or otherwise,
are strictly for the compiler.

Me, I prefer to keep the class name in the static references for the
most part. I don't know why so many people are so lazy about such
things - I read a lot of whining about "extra" typing. Try being the
maintenance programmer who has to debug the thing a year later and you
will truly appreciate leaving the class name in the expression.

Sometimes you have to think of others when you're coding.

Although I tend to agree to the whining part ;-) in this particular case
with a modern IDE like Eclipse you'll find the originating class as
easily in both cases (static imports really cut off just one more item
from a qualified name). Not working with such an IDE would be a far
bigger mistake than using or not using static imports. My 0.02 EUR.

Kind regards

robert
 
L

Lew

Robert said:
with a modern IDE like Eclipse you'll find the originating class as
easily in both cases (static imports really cut off just one more item
from a qualified name). Not working with such an IDE would be a far
bigger mistake than using or not using static imports. My 0.02 EUR.

Good point, and it cuts both ways. With a good IDE like that it'll insert the
expression with the class name with no more than a couple of Ctrl-Space /
click combinations.

Static imports are often quite useful and when it makes a program clearer
they're absolutely the right thing to use. The original question was about
run-time impact, of which there's none. The source-code question is always
about what makes the code easier to develop and maintain correctly.
 
R

Robert Klemme

Good point, and it cuts both ways. With a good IDE like that it'll
insert the expression with the class name with no more than a couple of
Ctrl-Space / click combinations.

Yepp, definitively.
Static imports are often quite useful and when it makes a program
clearer they're absolutely the right thing to use. The original
question was about run-time impact, of which there's none. The
source-code question is always about what makes the code easier to
develop and maintain correctly.

Full ack!

robert
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top