Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

question about resource and performance costs of static imports

Thread view: 
gwlucas@sonalysts.com - 13 Dec 2007 14:02 GMT
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);
  }
}
Daniel Dyer - 13 Dec 2007 14:16 GMT
> I am considering using static imports in my Java code and was
> wondering whether there was a performance cost or resource issues
[quoted text clipped - 11 lines]
> 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.

Signature

Daniel Dyer
http://www.uncommons.org

Lew - 13 Dec 2007 15:49 GMT
>> I am considering using static imports in my Java code and was
>> wondering whether there was a performance cost or resource issues
[quoted text clipped - 15 lines]
> 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.

Signature

Lew

Robert Klemme - 16 Dec 2007 13:00 GMT
>>> I am considering using static imports in my Java code and was
>>> wondering whether there was a performance cost or resource issues
[quoted text clipped - 26 lines]
>
> 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
Lew - 16 Dec 2007 19:40 GMT
> 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.

Signature

Lew

Robert Klemme - 16 Dec 2007 21:44 GMT
>> 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
[quoted text clipped - 4 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.