2

From http://cs-fundamentals.com/tech-interview/java/which-java-package-is-imported-by-default.php

In every Java program there can be one unnamed package, which is simply a package with no name. If you omit the package statement while writing the class definition, the class name is placed into the default package, which has no name. Java compiler automatically imports this package.

Is that true?

EDIT

My concerning about Java compiler automatically imports this package.

Aladdin
  • 1,207
  • 2
  • 16
  • 26
  • Yes, although it's discouraged to use the default package. – endowzoner Nov 11 '15 at 07:34
  • The default package is an unnamed package. The unnamed package contains java classes whose source files did not contain a package declaration. The purpose of default packahe is for convenience when developing small or temporary applications or when just beginning development.The compiled class files will be in the current working directory. The rest of your statement is also correct. – Lukas Hieronimus Adler Nov 11 '15 at 07:35

3 Answers3

8

No, the unnamed package is not imported by the compiler.

Please note the following from the Java Language Specification.

From Compilation Units:

A compilation unit that has no package declaration is part of an unnamed package (§7.4.2).

From Packages:

For small programs and casual development, a package can be unnamed (§7.4.2) or have a simple name, but if code is to be widely distributed, unique package names should be chosen using qualified names. This can prevent the conflicts that would otherwise occur if two development groups happened to pick the same package name and these packages were later to be used in a single program.

From Observability of a Package:

The packages java, java.lang, and java.io are always observable.

Note that the unnamed package is not listed.

From Import Declarations:

A type in an unnamed package (§7.4.2) has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported, and (b) static members of types in an unnamed package cannot be imported. As such, §7.5.1, §7.5.2, §7.5.3, and §7.5.4 all require a compile-time error on any attempt to import a type (or static member thereof) in an unnamed package.

Andreas
  • 154,647
  • 11
  • 152
  • 247
2

No. The statement

Java compiler automatically imports this package.

is not correct in several respects.

First of all, packages cannot be imported. §7.5 (JLS) says:

An import declaration allows a named type or a static member to be referred to by a simple name (§6.2) that consists of a single identifier.

This refers to named types and static members, only. Not packages! Although ... there is a convenient method to import every named type from a package with import my.pkg.*;.

Second, the compiler will not automaticaly import anything from the unnamed package. In fact, it is not possible to refer to any program element of the unnamed package from any named package.

I guess that this statement means that you have access to all elements in the same package (for which you are writing your code) without any import statements. But this is true for all packages.

Community
  • 1
  • 1
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
-1

Yes. If you have a project set up in Eclipse, Netbeans, or another IDE, you can attempt to add a class to a project without adding a package. It'll usually say something like "Adding classes to the default package is not recommended," but allow you to add that class to the default package anyway. Alternatively, you could attempt to run javac on a .java without a package, which would work assuming that all imports, syntax, etc. are correct.

Steven Hewitt
  • 302
  • 3
  • 12