Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 869
Pops Avatar asked Jan 08 '10 19:01

Pops


People also ask

Which is the default package import in java?

Java compiler imports java. lang package internally by default.

How do you import a class in java?

Otherwise, it is very easy. In Eclipse or NetBeans just write the class you want to use and press on Ctrl + Space . The IDE will automatically import the class.

What is default package and class in java?

The default package is a collection of java classes whose source files do not contain and package declarations. These packages act as the default package for such classes. It provides the ease of creating small applications when the development of any project or application has just begun.


1 Answers

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.

like image 196
Dan Dyer Avatar answered Oct 13 '22 10:10

Dan Dyer