What is the purpose of the * in java.io.*?
import java.io.*;
class Trial{
public static void main(String[]args){
System.out.println("Hello,World!");
}
}
What is the purpose of the * in java.io.*?
import java.io.*;
class Trial{
public static void main(String[]args){
System.out.println("Hello,World!");
}
}
The *
tells the compiler to import all top-level classes in java.io
on demand. The construct is called a type-import-on-demand declaration.
From JLS §7.5.2:
A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.
TypeImportOnDemandDeclaration: import PackageOrTypeName . * ;
So, for example, since you've included that import statement, you can use a class like java.io.File
without having to prefix the type name with java.io
; you can use the simple name File
.
The star indicates that all classes from the java.io package should be imported.
A wildcard in a package name import
is used to include all the classes contained in that specific package. Check official documentation.
In addition you are able to import inner static classes to be able to refer to them without a fully qualified name, eg:
import org.package.MyClass;
//MyClass.InnerClass inner; not needed
InnerClass inner;