shemj
shemj

Reputation: 31

What is the purpose of * in java.io.*

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!");
     }
}

Upvotes: 1

Views: 3685

Answers (3)

arshajii
arshajii

Reputation: 129517

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.

Upvotes: 1

Jack
Jack

Reputation: 133597

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;

Upvotes: 0

E. Anderson
E. Anderson

Reputation: 3493

The star indicates that all classes from the java.io package should be imported.

Upvotes: 0

Related Questions