The moose likes Beginning Java and the fly likes how does 'import' work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how does Watch "how does New topic

how does 'import' work

Gar Morley

Joined: Dec 15, 2004
Posts: 14
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

Joined: Apr 08, 2004
Posts: 130

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.

[ April 07, 2005: Message edited by: John Dell'Oso ]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
With other words, imports don't have any effect on class files, they just make writing java source code more convenient.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: how does 'import' work
It's not a secret anymore!