Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding import statement

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhere I have read
import java.*;
This example will compile, and it will try to import any classes within the directory java. It will not, however, import any of the packages located in subdirectories of java. In the preceding instance, there are no classes located directly in the Java package, therefore nothing will be imported.

But in the above coding,it did show compile error as stated


And consider the below program that worked successfully without any error.



And another class in different package


What is the rule behind using import java.* (or) import payroll.*; .Please help me.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the first code does not compile exactly for the reason you mentioned.

import java.*

does not import subpackages.

You cannot use the Date class because you did not import the package it is in.

The other code compiles because the class you created is part of the payroll package and you import it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic