Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

import time

  Asked By: Aditi    Date: Jul 02    Category: Java    Views: 642

I have been thinking of the amount of time a servlet takes to compile and
thought of a import statement.

If you are only using an ArrayList on a page, is it more effecient to have
"import java.util.ArrayList" or is "import java.util.*" just as good? I
mean, does importing the entire package make a difference in taking up more
processing time? I guess I never really understood how importing works
aside from providing use of the package.



2 Answers Found

Answer #1    Answered By: Ruairidh Anderson     Answered On: Jul 02

There is no penalty for importing  classes. All the
import instruction does is tell the compiler where to
look for classes it does not "add" the classes that
you do not use to you class.

Answer #2    Answered By: Jay Richards     Answered On: Jul 02

There is not any penalty when you use one of
the three possible ways to reference a class

The only thing we are doing with "import" is
telling to the compiler for what classes we dont
want to write the complete name, just that
(Remember that the complete name of a class is
the name it self plus the name of the package)

All of these ways are solved at compile  time,
not at run time, so there is not any
difference at run time.

The three ways, for a class named "MyClass" in a
package named "myPackage", are these:

1) To use the complete name of the class:
myPackage.MyClass a = new myPackage.MyClass ();

2) To use import  with *;
import myPackage.*;
MyClass a = new MyClass ();

2) To use import with the class name;
import myPackage.MyClass;
MyClass a = new MyClass ();

Didn't find what you were looking for? Find more on import time Or get search suggestion and latest updates.