Introduction / Import
Subtitles of the Movie
The import statement in Java is the cause of some confusion. The reason it's confusing is that it looks like one thing when it's actually another. In fact, the import statement could be the most misunderstood thing in Java. It's confusing mainly because import was such a poor choice of a name. When you think of importing something in software, you think of loading something from disk into a program, but that's not what the import statement does. Now, I'm going to list eight specific characteristics of the import statement. These eight things describe everything that import does. The import statement provides full names for things that you in your program only call by partial name. That's really all the import statement ever does. In reality, you're never required to use an import statement to write a program. It's only there for convenience. All it does is tell the compiler the full name of something that you call by a short name. If you always use the full names of everything, the import statements will simply be ignored. You can always use it to provide the full name of a class or interface from the Java library. For example, this import statement will allow you to call the class by its short name; image. The compiler will look at the import statement to find the full name. You put in a statement like this, then the compiler will look in the directory named Java with a subdirectory named AWT for a class file by the name of image. If that doesn't work, the compiler will look on the class path for the same set of names in a jar file. The point is that no matter what is written on the import statement, nothing happens until the short name is used in your program. You can use an asterisk in place of the class or interface name and include all the classes and interfaces that are in the same directory. For example, this import statement can be used to resolve the name image. But it will also resolve rectangle, button, canvas, color, lots of others. A hundred twenty-one in all. You have to be sure that you don't have a name conflict. It's inconvenient when a name resolves the wrong class. Beware of the asterisk. The asterisk is not a wild card in the traditional sense. It won't insert intermediate names. An asterisk only marks the place where a class or interface name is inserted to build a full name. For example, this import statement works to find things in the AWT package, but not for things in the sub-package. For example, this class can not have its name resolved with that import statement. It requires that you include the rest of the path name with the import statement like this. That's because all the compiler does is replace the asterisk with a class name and try it. The import statement has no effect on the running of your program. The import statement is only information for the compiler. Once the compiler has determined the full names of all the classes and interfaces, the import statement is thrown away. There is no runtime overhead whatsoever. There is one import statement that is always present by default. It is as if this import statement is at the top of every program. Every class and interface of the Java lang package are always available by their short names. These are things like math and string and a number of errors and exceptions. A hundred eight classes and interfaces in all. Ok. Up until now, I've been describing things that have been a part of the import statement since the very first days of Java. All this is still valid and still works the same as it always has. But in Java 5 something new was added. You can now import the static members of a class and refer to them as if they were members of the local class. For example, the byte class has a static method named value of that converts a string into a byte value. You've always been able to call that method this way. Now you can specify it on the import statement like this. Notice the presence of the static keyword on the import statement. Once you have this import statement in place, you can call the method this way. You can import all the static members of a class, the methods and the variables by using an asterisk. I'll have some more to say about this later in the course in the section of gotchas.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 