• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static imports / program efficiency

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while doing some reading, i read about the idea of static imports. I'm wondering how it's really beneficial for the overall program other than saving you a few keystrokes. The examples that they show in the book are as follows:



what did those imports really accomplish? I guess it saved time by not having to type:



is that really necessary? I guess if you had a lot of mathmatical operations/calculations it may help so you don't have to type the class name each and every time. But why the static identifier? Doesn't the compiler already know that they're static methods?

With this being said, is it better to import a specific class that you're going to use instead of the entire package?

For example:



Instead of:



is it more efficient to only import the class and better design practice to be specific as possible? Does that help with memory usage? Or does it really matter with today's hardware capabilities?
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all what you do or do not import will have no impact on the performance of your application at runtime or the memory footprint. The purpose of import is to, for all intents and purposes, tell the compiler what you're talking about. If I import java.util.List I'm telling the compiler that when I refer to a List that's what I'm talking about. The new "static import" allows you to take it a step further and import the members of a class as well. It's of debateable usefulness. When used improperly it can make code harder to read as you try and figure where this method or variable came from. Other times it makes it easier by not forcing you to qualify the variable.

As for the second question: yes it is better to import a specific class. If you're writing small programs to learn Java or for school then it doesn't matter. Heck, if you're doing it in Notepad then I say use * and more power to you, no sense wasting all that time. However, in the "real world" it's not a good practice. Import a single class at a time, you're less likely to have namespace conflicts or to have a developer in the future have to try and figure out which package it's coming from. Chances are you'll be using an IDE too and it should have support to deal with the imports for you.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brian LaRue:
what did those imports really accomplish? I guess it saved time by not having to type:



Yes, and that's the only reason for *all* imports - having to type less. As Ken already hinted at, import statements don't have any impact on the generated byte code - they are pure "syntactic sugar".
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:

Yes, and that's the only reason for *all* imports - having to type less.



Not to mention that provided there is no ambiguity, the shorter, unqualified names of classes and methods are much easier to read then the fully qualified versions.
 
reply
    Bookmark Topic Watch Topic
  • New Topic