82

Is it possible to import a class in Java which is in the default package? If so, what is the syntax? For example, if you have

package foo.bar;

public class SomeClass {
    // ...

in one file, you can write

package baz.fonz;

import foo.bar.SomeClass;

public class AnotherClass {
    SomeClass sc = new SomeClass();
    // ...

in another file. But what if SomeClass.java does not contain a package declaration? How would you refer to SomeClass in AnotherClass?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Pops
  • 30,199
  • 37
  • 136
  • 151
  • 1
    I believe you just refer to it. If they are in the default package, it means they must be in the same directory (or at least on the class path). – Topher Fangio Jan 08 '10 at 19:33
  • 1
    @Thorbjørn: I'm in a training session right now and nobody knows how to do it after five minutes of poking. Initially, the instructor was trying to illustrate that Eclipse can add missing import statements with one click, but the tool didn't pick up a class with no package. – Pops Jan 08 '10 at 19:39
  • @mmyers: Hm, I'm wary of that because I know zero about Groovy. Even so, looks like a failure of my SO-search-fu. Thanks! – Pops Jan 08 '10 at 19:43
  • Every class has a package. If you don't declare the package, it gets added to the 'default' package like so: "import default.Foo". Classes that are included automatically are in java.lang and can be imported explicitly "import java.lang.String" but would be redundant. – Kelly S. French Jan 08 '10 at 19:45
  • I think the title is a little misleading: "...import a class from the default package..." would be better (not a native english speaker, I may be wrong) – user85421 Jan 08 '10 at 19:57
  • @Carlos: actually, that brings up another question: Are default packages in different files all treated as being in the same package? I'll look it up and report back. – Pops Jan 08 '10 at 20:11
  • @Carlos: Per JLS 7.4.2: "A compilation unit that has no package declaration is part of an unnamed package." That implies to me that they're not all the same default package. – Pops Jan 08 '10 at 20:19

5 Answers5

157

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.

Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
59

The only way to access classes in the default package is from another class in the default package. In that case, don't bother to import it, just refer to it directly.

Dan
  • 10,990
  • 7
  • 51
  • 80
18

That's not possible.

The alternative is using reflection:

 Class.forName("SomeClass").getMethod("someMethod").invoke(null);
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • That assumes the class is in a package in the CLASSPATH. If it were in the CLASSPATH, Eclipse would have found it and the trainees wouldn't have had to ask. – Kelly S. French Jan 08 '10 at 19:46
  • 7
    @Kelly, are you absolutely sure about that? – Thorbjørn Ravn Andersen Feb 13 '11 at 13:42
  • This will cause a `NullPointerEsception` because of the null object parameter. – user207421 Oct 11 '17 at 00:22
  • @KellyS.French No. The class isn't in the current package, and can't be imported. Eclipse cannot help. – user207421 Oct 11 '17 at 00:23
  • @EJP that was my point. If the desired class is not mentioned (reachable?) by the current classloader then it won't help to try to use Class.forName. What you would have to do is instantiate a new classloader which has been provided with the path to the class file, as in the class file or a JAR, etc. and use that classloader to request the desired class. The assumption is if the class is in the default package then it should be loadable the way Oscar mentions. – Kelly S. French Oct 13 '17 at 19:42
  • Tis actually works - at least for me using Java 8. – Rhubarb May 08 '20 at 12:25
6

As others have said, this is bad practice, but if you don't have a choice because you need to integrate with a third-party library that uses the default package, then you could create your own class in the default package and access the other class that way. Classes in the default package basically share a single namespace, so you can access the other class even if it resides in a separate JAR file. Just make sure the JAR file is in the classpath.

This trick doesn't work if your class is not in the default package.

Rob H
  • 14,502
  • 8
  • 42
  • 45
4

It is not a compilation error at all! You can import a default package to a default package class only.

If you do so for another package, then it shall be a compilation error.

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
  • 3
    This is incorrect. There is no syntax that will allow you to name a class in the default package in an `import` statement. You can refer to a class in the default package from another class in the default package **without** importing it, but that is not what the question is asking. (And it is not what your answer literally says either!) – Stephen C Nov 03 '18 at 06:20