tags:

views:

113

answers:

6

Is loading only the needed classes directly a good way of reducing the overall memory usage of a Java application?

For example:

import java.awt.Graphics;

vs

import java.awt.*;
+13  A: 

No. You should import only the needed classes to make it clear to programmers which classes are actually needed by your class.

Import statements just tell the compiler where to look for the classes you are using - it doesn't mean all the classes in the package are loaded into memory.

Brabster
More importantly, you put the full name of the classes you're importing in order to avoid any name conflicts: http://stackoverflow.com/questions/187453/import-package-vs-import-package-specifictype
Kavon Farvardin
...make it clear to _compilers_!
Thorbjørn Ravn Andersen
Perhaps it would be good to mention too...modern IDEs take care of the chore of adding the imports for you so there really is little reason not to use fully qualified imports.
Kevin Brock
@Thorbjorn no, I did mean to say programmers. I don't think the compiler cares either way. A programmer, on the other hand, can learn more about a class from a set of specific imports than they can from *'d packages.
Brabster
+6  A: 

To put it simply: no.

import statements aren't translated into any form of bytecode. They're just shortcuts to avoid using (ugly!) fully qualified type names.

Martinho Fernandes
+1  A: 

Did you mean star imports like import pack.*;?

In Java, it has nothing do with memory usage, import is only used to change how you reference the classes. However, there are coding practice concerns regarding star imports.

SHiNKiROU
A: 

Explicitly importing the classes you need vs doing a *-import makes no difference. The JVM only loads what you end up using and nothing more.

Having said that, explicitly importing your classes is a good software design decision (and *-import is the sign of slackers who don't know or care about good software design principles... I'm not joking, I'm dead serious.)

luis.espinal
I care about good software design principles. I use star imports from time to time. I'm not a slacker. It is totally a matter of *coding* style. It has nothing to do with software *design*.
Martinho Fernandes
To me, using star imports is a matter of environment. Am I hacking away in Notepad? Gimme them lurvly stars imports. Am I writing real programs? Let the IDE handle it (and by God if it doesn't do single imports I swear I'll
gustafc
+3  A: 

As others have put it, imports are used by the compiler only. You COULD write your whole program without any imports by using the full names of everything, but that would quickly grow rather big.

java.io.InputStream is = new java.io.FileInputStream(new java.io.File("foo"));

The star-import is to make it less tedious to write all the import statements by hand, but result in too many things being imported, so that the compiler have more than one possibility. Modern IDE's like Eclipse therefore import everything one-by-one so this cannot happen.

Thorbjørn Ravn Andersen
+1  A: 

I generally use fully-qualified type names, rather than such large statements. Packages exist for a reason, and it's because the developer of that package likely filled his package namespace with all sorts of crap that you don't like, and you just want that class.

DeadMG