0
java.io.Reader.*;

I know that Reader is a class, not a package.

So, what the above declaration will import?

kevin gomes
  • 1,775
  • 5
  • 22
  • 30
  • Refer [this](http://stackoverflow.com/questions/12620369/how-java-import-works) and [this](http://stackoverflow.com/questions/1053658/how-is-import-done-in-java) – Madhan Jul 05 '15 at 17:20
  • Just to be a bit technical: Java has [_import declarations_](http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.5), not import [_statements_](http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.5). However, the code fragments you posted are neither. The first would be legal as part of an `import` declaration and the second as part of an `import static` declaration. – Ted Hopp Jul 05 '15 at 17:24
  • @TedHopp : `java.io.Reader.*` does not import static members of the class. – kevin gomes Jul 05 '15 at 17:33
  • @kevingomes - `java.io.Reader.*;` is not a declaration at all and it is impossible to say what it might mean until you tell us whether it is part of a type import (`import java.io.Reader.*;`) or a static import (`import static java.io.Reader.*;`). – Ted Hopp Jul 05 '15 at 17:35
  • @TedHopp : I think I am confusing you. Please check my edited straight forward question – kevin gomes Jul 05 '15 at 17:38
  • You keep changing your question. As it reads now, the answer is that it will import nothing, since `java.io.Reader` has no (visible) subclasses. – Ted Hopp Jul 05 '15 at 17:39
  • @TedHopp : That's all I wanted to know. Previously I don't know what you were saying. Make an answer of your last comment – kevin gomes Jul 05 '15 at 17:41

3 Answers3

2

In the same file you can have both

import java.io.Reader;          //Statement 1

import static java.io.Reader.*;        //Statement 2

the first one is importing only the class Reader from package java.io, the second one is importing all the static members of class Reader, wich appears to be only

private static final int maxSkipBufferSize = 8192;

so, pretty useless, because being it private you cannot access it from your class, neither for reading nor for modifying

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
  • are `import static java.io.Reader.*` and `import java.io.Reader.*` same? – kevin gomes Jul 05 '15 at 17:28
  • the scond one will result in a compiler error, but you can write `import java.io.*` to import all classes in the package `java.io` – Luigi Cortese Jul 05 '15 at 17:29
  • @kevingomes - They are not at all the same. Read [Section 7.5 of the Java Language Specification](http://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.5) to learn all about it. – Ted Hopp Jul 05 '15 at 17:31
  • @LuigiCortese - The second one doesn't result in a compiler error. Rather, since `Reader` has no static members, the statement will simply be ignored by the compiler. – Ted Hopp Jul 05 '15 at 19:06
2

The declaration:

import java.io.Reader.*;

is an example of a type-import-on-demand declaration. From the Java Language Specification:

A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.

Since java.io.Reader contains no nested classes or other accessible types, the declaration would simply be ignored.

Note that Java also has an import static declaration. So

import static java.io.Reader.*;

would be an example of a static-import-on-demand declaration. Again, according to the Java Language Specification:

A static-import-on-demand declaration allows all accessible static members of a named type to be imported as needed.

And since java.io.Reader also has no accessible static members, the declaration would again be ignored.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • will it import any `subclass` of the imported class (IF ANY) – kevin gomes Jul 05 '15 at 17:45
  • @kevingomes - Yes, it will import (on demand) any subclasses of the imported class, provided they are accessible. Note that it will not import `java.io.Reader` itself, however. You'd need a separate `import` declaration for that. – Ted Hopp Jul 05 '15 at 17:47
  • `Provided they are accessible`, means? – kevin gomes Jul 05 '15 at 17:48
  • 1
    @kevingomes - If a subclass is `private`, it is not accessible. if it has default access (no qualifier) then it is accessible only to members of the `java.io` package; if it is `protected`, it is accessible to members of the `java.io` package and to any subtypes of `java.io.Reader` regardless of package; if it is `public` then it is accessible to any code. See [Controlling Access to Members of a Class](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html). – Ted Hopp Jul 05 '15 at 17:50
  • You said, `If a subclass is private, it is not accessible`. As far as I know, `private` modifier is not allowed before a class name i.e. `private class-name {}`, is not allowed – kevin gomes Jul 05 '15 at 17:56
  • @kevingomes - First, I misstated things. A `private` subclass is accessible to other members of the containing class. Second, you are correct that top-level classes can only have `public` or default access, but I was talking about _nested_ classes. All access modifiers are allowed for nested classes (whether they are static nested classes or inner classes). The link at the end of my previous comment discusses this distinction. – Ted Hopp Jul 05 '15 at 18:14
1

Statement 1 will include the Reader Class that you can use in your code, as this class is used to read the character stream

Statement 2 will include all the Classes from Reader.* Package ( if it is a package), i am assuming it generic

Shahbaz
  • 73
  • 8
  • The meaning of declaration 2 would depend on whether this is an `import` or `import static` declaration. (However, since `java.io.Reader` has no accessible nested classes or static members, it's pretty much a useless declaration in either case.) – Ted Hopp Jul 05 '15 at 17:30