• Post Reply Bookmark Topic Watch Topic
  • New Topic

import statement  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the difference between importing a specific package API, and the general API like the following:

import java.sql.ResultSet;
and import java.sql.*;

Can I just use import java.sql.*; to cover java.sql.SQLException, java.sql.PreparedStatement as well?
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. That's exactly the purpose of using *.

import java.sql.ResultSet; will import only ResultSet. But import java.sql.*; will import everything in sql.
[ September 24, 2004: Message edited by: marc weber ]
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most coding guidelines recommend using class names instead of *. That provides better documentation and avoids problems where the same class name is in two or more packages. I love the Eclipse feature to "organize imports" cause it lets me use *s for a while then fix them up to real class names. I've had developers complain that using class names made the import list too long. I suggested that might be telling them something about their design.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.sql.*; will import everything in sql.

To be clear, import java.sql.*; would import every class, interface and enum in the java.sql package.

Ditto to what Stan said.
[ September 24, 2004: Message edited by: Dirk Schreckmann ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan and Dirk: Good points! Thank you for expanding on this.
[ September 25, 2004: Message edited by: marc weber ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!