What's the Syntax to Import a Class in a Default Package in Java

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 static import a class from default package?

This is impossible with java, you have to package them in a unique or different package.

Or you can use :

System.out.println(A.x);

You can read more in java doc about Import Declarations

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.

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 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".

Eclipse + Java: How do I import classes from the default package?

You don't.

Sorry.

Though I'm quoting the other answer, you can check out the ORACLE reference that says that that is intended behaviour.

I recommend moving your stuff to a named package.

If you absolutely cannot re-factor, you can try using reflection to access it. Again, check the answer linked above.



Related Topics



Leave a reply



Submit