• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

how does 'import' work

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic