How to Access Java-Classes in the Default-Package

What's the syntax to import a class in a default package in Java?

You can't import classes from the default package. You should avoid using the default package except for very small example programs.

From the Java language specification:

It is a compile
time error to import a type from the
unnamed package.

How to import a class from default package

From the Java language spec:

It is a compile time error to import a type from the unnamed package.

You'll have to access the class via reflection or some other indirect method.

How import java classes inside the default package from the default package

By default, the classes in the same package are already imported. You don't need to declare any import for this.
So to access to the default package classes can only be done by classes in the default package.

How to access java-classes in the default-package?

You can’t use classes in the default package from a named package.

(Technically you can, as shown in Sharique Abdullah's answer through reflection API, but classes from the unnamed namespace are not in scope in an import declaration)

Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:

import Unfinished;

That's no longer allowed. So to access a default package class from within a packaged class requires moving the default package class into a package of its own.

If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning.


Update 2014: bug 6975015, for JDK7 and JDK8, describe an even stricter prohibition against import from unnamed package.

The TypeName must be the canonical name of a class type, interface type, enum type, or annotation type.

The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type is a member of a named package, or a compile-time error occurs.


Andreas points out in the comments:

"why is [the default package] there in the first place? design error?"

No, it's deliberate.

JLS 7.4.2. Unnamed Packages says: "Unnamed packages are provided by the Java SE platform principally for convenience when developing small or temporary applications or when just beginning development".

How does one use a class from a default package in Java?

I think this could be useful
What's the syntax to import a class in a default package in Java?

Anyway, if I got it correctly, you have to add it to the java path

Getting started. To use this class, you must have StdOut.class in your Java classpath. If you used our autoinstaller, you should be all set. Otherwise, either download stdlib.jar and add to your Java classpath or download StdOut.java and put a copy in your working directory.

Accessing class in the default package from outside the jar

You can not access classes in the default package from a named package.

Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:

import Unfinished;

This is no longer allowed

See https://bugs.openjdk.java.net/browse/JDK-6975015

Can't access Classes in default package

How are you creating your classes? When you create a new class, Eclipse shows a New Java Class dialog. This dialog lets you choose which package to create your new class in:

Sample Image

Just click the Browse button and you'll be able to pick a package.

enter image description here no i do not like image descriptions >:(

How to access java-classes in the default-package?

You can’t use classes in the default package from a named package.

(Technically you can, as shown in Sharique Abdullah's answer through reflection API, but classes from the unnamed namespace are not in scope in an import declaration)

Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:

import Unfinished;

That's no longer allowed. So to access a default package class from within a packaged class requires moving the default package class into a package of its own.

If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning.


Update 2014: bug 6975015, for JDK7 and JDK8, describe an even stricter prohibition against import from unnamed package.

The TypeName must be the canonical name of a class type, interface type, enum type, or annotation type.

The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type is a member of a named package, or a compile-time error occurs.


Andreas points out in the comments:

"why is [the default package] there in the first place? design error?"

No, it's deliberate.

JLS 7.4.2. Unnamed Packages says: "Unnamed packages are provided by the Java SE platform principally for convenience when developing small or temporary applications or when just beginning development".



Related Topics



Leave a reply



Submit