• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how does 'import' work

 
Gar Morley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I want to write a program with StringTokenizer (for example) and write 'import java.util.*;' near the top of my file, is the compiler including *everything* from java.util or does it just choose what has been mentioned in the code?
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gar,

The import statement does not do any "including". It is a (very) convenient way for the compiler to find classes that are required when compiling your class. It also cuts down on a lot of typing. For example, if you choose to not use an import statement for the StringTokenizer class, you would have to qualify the package name of the class every time you use it in your code:

e.g. java.util.StringTokenizer instead of just the class name StringTokenizer.

The '*' in java.util.* means import all classes in the java.util package. Again, the '*' is a wildcard used for convenience. If you were using more than one class from the java.util package, you would have to specify more than one import statement for each class you were using if you chose to not use a wildcard import.

Regards,
JD
[ April 07, 2005: Message edited by: John Dell'Oso ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words, imports don't have any effect on class files, they just make writing java source code more convenient.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic