Reputation: 1378
Can any one clearly explain to me what exactly happens when we use the import statement in Java files? Does it increase the size of the file if we add more and more java classes? Why don't we use class loader for the same? And what are the restrictions for importing?
Upvotes: 22
Views: 78163
Reputation:
The import statement in Java allows to refer to classes which are declared in other packages to be accessed without referring to the full package name. You do not need any import statement if you are willing to always refer to java.util.List by its full name, and so on for all other classes. But if you want to refer to it as List, you need to import it, so that the compiler knows which List you are referring to.
Classes from the java.lang package are automatically imported, so you do not need to explicitly do this, to refer to String, for example.
Read more: http://wiki.answers.com/Q/Why_import_statement_is_needed_in_Java_program#ixzz1zDh2ZBhE
Upvotes: 0
Reputation: 383746
import
declarations (not statements) are essentially short-hand enabler at the source code level: it allows you to refer to a type or a static
member using a single identifier (e.g. List
, min
) as opposed to the fully qualified name (e.g. java.util.List
, Math.min
).
import
declaration section is a compile-time element of the source codes, and has no presence at run-time. In JVM bytecodes, type names are always fully qualified, and unless you're using a poorly written compiler, the binary should only contain names for types that are actually being used.
Class loaders are used for an entirely different concept, and has nothing to do with import
feature at all.
An import declaration allows a
static
member or a named type to be referred to by a simple name that consists of a single identifier. Without the use of an appropriateimport
declaration, the only way to refer to a type declared in another package, or astatic
member of another type, is to use a fully qualified name.A single-type-import declaration imports a single named type, by mentioning its canonical name.
A type-import-on-demand declaration imports all the accessible types of a named type or package as needed. It is a compile time error to import a type from the unnamed package.
A single static import declaration imports all accessible static members with a given name from a type, by giving its canonical name.
A static-import-on-demand declaration imports all accessible static members of a named type as needed.
import
related questionsOn the grammatical role of import
:
import
called? - it's a declaration, not a statementOn on-demand vs single-type:
import java.util.*;
and import java.util.Date;
?On import static
:
static
modifier after import
mean?Performance-related issues:
Upvotes: 49
Reputation: 7078
Packages consist of classes, classes in a package consist of methods, variables etc etc. A class has a full name which comprises of the package name and the class name. If you need to use a class in your code,you need to give the compiler the full name of the class.So, you use an import statement OR you can type the fully qualified name every place you use that class in your code.
For example, if you need an AraryList
in your code, you use the import statement import java.util.ArrayList;
instead of typing the fully qualified class name every place you need an Arraylist.
For more detailed info, see JLS.
Upvotes: 5
Reputation: 6494
The imports in java are only hints for the compiler. It doesn't affect the size of the binary class file at all. You can either use an imports once or write the full name of the Class every time you use it.
Imports are just a concession to readability and the laziness of the developer.
Upvotes: 4
Reputation: 8116
import
statements say to the compiler: if you have a function that cannot be found in this class look in the list of imports.
This way you can refer to functions in other packages without having to copy the definition (like C(++) .h files) to your own package.
Upvotes: 3