This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import servlet packages

 
Kallol Das
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.servlet.*;
import javax.servlet.http.*;

My question is:

javax.servlet is the parent package which contains javax.servlet.http package,then 1st line is sufficient to import both the packages,Right? then why there is a requirement to separately import javax.servlet.http package?(in the books like Complete reference there is mention of both these import statements to import javax.servlet.http package)
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

then 1st line is sufficient to import both the packages,Right?


No. Importing a package does not recursively import any child packages, so both import statements are necessary.
 
Srikanth Ramu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imports can be of 2 types:

1. Specifying the whole Class like "import java.util.ArrayList;"

2. Specifying the Package name like "java.util.*;"

For more details refer:
http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#70209
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srikanth Ramu:
Imports can be of 2 types...
For more details refer:
http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#70209

That link is to the old second edition of the JLS. The third edition introduced static imports, so there are now four types of import statements...
  • 7.5.1 Single-Type-Import Declaration
  • 7.5.2 Type-Import-on-Demand Declaration
  • 7.5.3 Single Static Import Declaration
  • 7.5.4 Static-Import-on-Demand Declaration
  • Addressing the original question, JLS - 7.5 Import Declarations states, "A type-import-on-demand declaration (�7.5.2) imports all the accessible (�6.6) types of a named type or package as needed." Packages are not types.
    [ April 24, 2007: Message edited by: marc weber ]
     
    Kallol Das
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thnx guys for your informative inputs.
    I have joined this ranch today.I am humbled by the kind of(prompt and quality) response I got.thank you very much.





    Kallol
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic