jax
jax

Reputation: 38583

Will using a single file from a package include all files?

If I import and use a single class from another package, will it include all files from the package?

for example, if I use only a single class from the mylibrary package which is called MyFunctions, will the following include all classes or not?

import my.example.mylibrary.*;

compared with:

import my.example.mylibrary.MyFunctions;

Upvotes: 2

Views: 451

Answers (4)

Jacob Mattison
Jacob Mattison

Reputation: 51052

If you specifically import MyFunctions, the compiler still will not know about MyOtherFunctions. The * version will import everything from the package.

Upvotes: 1

OscarRyz
OscarRyz

Reputation: 199215

No

The import does not include anything into your program.

Classes are loaded when they are used, and not before. So the only thing the import is to help you to avoid typing the whole class name.

For instance it is much better to type ( and to read ) :

import my.example.mylibrary.SomeClass;
import java.util.List;
import java.util.ArrayList;
...
SomeClass some = new SomeClass();
List<SomeClass> list = new ArrayList<SomeClass>();
list.add( some );
etc.. 

than

// no import 
... 
my.example.mylibrary.SomeClass some = new my.example.mylibrary.SomeClass();
java.util.List<my.example.mylibrary.SomeClass> list = new java.util.ArrayList<my.example.mylibrary.SomeClass>();
list.add( some );

And yet, they perform exactly the same.

Also, bear in mind that using

import some.packagename.*;

vs.

import some.packagename.Each;
import some.packagename.Single;
import some.packagename.Class;
import some.packagename.ByLine;

Have exactly the same performace, except the firs it faster ( and dirtier ) and the second is clearer.

Always!! use the second form in production and/or when someone else needs to see your code.

Upvotes: 4

L. Cornelius Dol
L. Cornelius Dol

Reputation: 64026

Import statements are only a directive to the compiler to help it resolve references to classes using an unqualified class name - nothing more.

For example, every class implicitly imports java.lang.*, which means you can refer to String instead of java.lang.String and Integer instead of java.lang.Integer, etc.

If you have mypackage.A and mypackage.B and you import mypackage.A you can use A in the class but you must use mypackage.B instead of just B.

Using * instead of a specific class name simply tells the compiler to allow all classes in the package instead of a specific one. So import mypackage.* allows you to use A and B in the code.

Upvotes: 2

Thomas
Thomas

Reputation: 181745

What do you mean by "include"?

The two have no difference in performance. The only thing they do is allow you to write MyFunctions instead of my.example.library.MyFunctions all the time.

The class loader will (typically) load a .class file when you first use that class. When the .class is inside a .jar file, maybe it will load more than that. But that does not depend on the way you wrote your import.

Upvotes: 2

Related Questions