Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of the import statement in a Java file

Tags:

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?

like image 940
Pedantic Avatar asked May 25 '10 08:05

Pedantic


People also ask

What is an import statement in Java?

Import statements have to be the first code in a Java source file. An import statement tells Java which class you mean when you use a short name (like List ). It tells Java where to find the definition of that class. You can import just the classes you need from a package as shown below.

What does an import statement do?

The import statement can be used to import an entire package or sometimes import certain classes and interfaces inside the package. The import statement is written before the class definition and after the package statement(if there is any). Also, the import statement is optional.


2 Answers

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.

JLS 7.5 Import Declarations

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 appropriate import declaration, the only way to refer to a type declared in another package, or a static 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.

References

  • JLS 7.5.1 Single-Type-Import Declaration
  • JLS 7.5.2 Type-Import-on-Demand Declaration
  • JLS 7.5.3 Single Static Import Declaration
  • JLS 7.5.4 Static-Import-on-Demand Declaration

See also

  • Java Tutorials/Using package members
  • Java Language Guide/static import

Various import related questions

On the grammatical role of import:

  • What is an import called? - it's a declaration, not a statement

On on-demand vs single-type:

  • Import package.* vs import package.SpecificType
  • Why is using a wild card with a Java import statement bad?
  • What’s the difference between import java.util.*; and import java.util.Date;?

On import static:

  • What does the static modifier after import mean?
  • What is a good use case for static import of methods?
  • Should I use static import?

Performance-related issues:

  • Does importing of packages change visibility of classes? - ABSOLUTELY NOT!
  • Do multiple import statements in a program affect performance? - NOPE!
  • Any reason to clean up unused imports in Java, other than reducing clutter?
like image 99
polygenelubricants Avatar answered Oct 02 '22 15:10

polygenelubricants


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.

like image 23
Zaki Avatar answered Oct 02 '22 16:10

Zaki