Valhalla Legends Archive

Programming => General Programming => Java Programming => Topic started by: po0f on December 25, 2004, 03:28 AM

Title: imports
Post by: po0f on December 25, 2004, 03:28 AM
Is there any overhead incurred from importing all of the classes from a package? Example:
import java.somethingcool.*;
As opposed to just the class(es) you'll use:
import java.somethingcool.SomeClassA;
import java.somethingcool.SomeClassB;
Title: Re: imports
Post by: MyndFyre on December 25, 2004, 03:37 AM
No; that information is only used at compile-time.

Generally I believe that programmers prefer to just import a single class as opposed to an entire package to avoid naming conflicts.
Title: Re: imports
Post by: Kp on December 25, 2004, 11:43 AM
IIRC, there is a compile-time overhead associated with importing *, since the compiler must go determine what classes you're summoning and bring in enough of them that it can satisfy any requests to them.  As Myndfyre said, there's no runtime overhead to it.
Title: Re: imports
Post by: iago on December 26, 2004, 01:59 PM
They're right, there's no runtime overhead, but there are coding style issues.

I personally import classes seperately.  Why? Take for example code that does:

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;

And then you see a call like this:
SocketInputStream s = new SocketInputStream(); 
(that doesn't really exist, but shh)
If you want to look up documentation, where do you go?

If the imports looked like:
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.net.Socket;
import java.net.INetAddress

Then you can look at the imports section and instantly see where it comes from.  If it's not listed in the imports, then you know it's coming from the same packet as the class you're looking at, and can check there.

This is less of a concern if you're using an IDE, but it's still nice (in my opinion) to be able to look up any class you see referenced in the file and know instantly where it came from.  * imports annoy me a lot.