3

If, in import java.* the * includes all the packages, then why do have to write import java.lang.* and import java.util.*?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Android Dev
  • 1,496
  • 1
  • 13
  • 25
  • Classes only. If you use `import static`, it will import `static` members of a class. – Luiggi Mendoza Aug 24 '15 at 17:11
  • java.* will include the class in the java folder only not in its sub folders so you have to specify the class in sub folders – Raghavendra Aug 24 '15 at 17:15
  • possible duplicate of [Why is using a wild card with a Java import statement bad?](http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad) – Bacteria Aug 24 '15 at 17:42

3 Answers3

5
import packagename.*;

imports all classes, interfaces ect. from the package packagename but not "subpackages", i.e. import java.*; imports all classes from the java package, but no classes from java.util. Since there are no classes in the java package, using import java.* imports nothing.

fabian
  • 80,457
  • 12
  • 86
  • 114
1

Even though typically packages map to file system directories, there is not a hierarchical relationship between packages. A package com.initech is not the parent of com.initech.tps, they are two separate packages with no relationship between them. You can't refer to multiple subpackages with a wildcard because as far as the compiler is concerned there is no such thing as a "subpackage". java.lang is a package, java.util is a package, but java is not a package.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

Let's take an example Suppose you have one package called base.. And inside it you have package called child1.. Base package also have two classes in it..b1 and b2 And your child1 package also have some classes into it..c1,c2.. Then when you write import base.* it only imports classes of base package i.e b1 and b2 not the child package classes... And when you write import base.child1.* It imports all classes in child1 package....

Mr. Noddy
  • 1,530
  • 2
  • 16
  • 45