• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on use of imports  RSS feed

 
David A King
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused on the use of imports. I am reading a book on java and in one of their examples it starts a program with;

import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

The question I have is on the awt import.

If I have import java.awt.*;
I assume that that will include all I need under the awt directory but the next line, import java.awt.event.* says I am wrong in my assumption.

Can someone explain to me why the java.awt.*; does not include the .event.*; directory as well?

Thanks,

*Dave
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i recall correctly, it does not recurse down the directory structure. you get everything in the java.awt, but not what is in the sub-directories.
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave,

Very good question!

This is the way java packaging hierarchies are defined. Using Java Packages

java.awt.* => This package itself has classes within it. By using this import statement, this will include only the classes within the java.awt package. java.awt package summary
where as
java.awt.event.* => Like the previous package, this package has also classes within it. By using this specific import, it will only include the classes within the java.awt.event package. java.awt.event package summary

For example, I am working with CardLayout class in the java.awt package in my application. So, why should I be loading the ActionEvent class from the awt.event package when I am not going to use it?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That explanation does not really address the question. Classes are not imported unless they are needed. You can write as many import declarations as you like, but those not used in the code are not used (that isn't quite a circular argument, even though it looks like one).
The imports work like that because somebody decided they should work like that. That was 18+ years ago and we may not be able to find the reason now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!