Difference between java.util.Scanner and java.util.Scanner.*

// imports all classes of util package    
import java.util.*;

// imports Scanner class of util package
import java.util.Scanner;

// what does this do?
import java.util.Scanner.*;

Is Scanner a package here?

Edit: Ok so import java.util.Scanner.* imports the public nested classes. But what if there was also a package called Scanner? What would the statement import java.util.Scanner.* do then?

Answers


import java.util.Scanner;

This imports Scanner (as you already know).

import java.util.Scanner.*;

This imports any public nested classes defined within Scanner.

This particular import statement is useless, as Scanner does not define any nested classes (and the import does not import Scanner itself). However, this can be used with something like import java.util.Map.*, in which case Entry (an interface nested in Map that is commonly used when dealing with maps) will be imported. I'm sure there are better examples, this is just the one that came to mind.

All of this is specified in JLS §7.5 (specifically, see §7.5.1: Single-Type-Import Declarations).


In response to the OP's edit:

Ok so import java.util.Scanner.* imports the public nested classes. But what if there was also a package called Scanner? What would the statement import java.util.Scanner.* do then?

In this case there would be a compilation error, since the package java.util.Scanner would collide with the type java.util.Scanner.


Need Your Help

Importing bootstrap_and_overrides.css.less into scss mainfest file

ruby-on-rails twitter-bootstrap sass less asset-pipeline

I'm trying to organize my stylesheets like in #268 RailsCasts episode. My application.css.scss file looks like:

Create Android APK Test Application

android testing apk robotium

I have an app to be tested, called app A.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.