Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java imports clarification needed

Tags:

java

import

Since I'm still new to Java, I'm running into some need for clarification how imports should be done properly.

First:

import java.awt.*;
import java.swing.*;

I'm assuming the * means anything under the "awt" and "swing" directory. But I've seen people doing this before:

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

Am I misunderstanding something? Or am I simply finding redundancy?

like image 208
coffeemonitor Avatar asked Dec 27 '22 12:12

coffeemonitor


1 Answers

import javax.awt.*

Will include all of the classes in the package javax.awt however it will not inclue any other packages nested inside of javax.awt such as javax.awt.event. You need to use that as a separate import. All packages of different names needed to be included separately.

Using import javax.swing.* you will not need the following imports:

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

because they are direct children of the swing package.

like image 186
Matt Clark Avatar answered Dec 29 '22 01:12

Matt Clark