Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import java class? [duplicate]

Tags:

java

import

Possible Duplicate:
Import package.* vs import package.SpecificType

Can I do:

import java.awt.*

instead of:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

if both ways are correct which one is better?

like image 794
Registered User Avatar asked Feb 13 '10 00:02

Registered User


2 Answers

You can import the general package, but it's better to be more explicit and import the specific classes you need. It helps to prevent namespace collision issues and is much nicer.

Also, if you're using Eclipse and the shortcut CTRL+SHIFT+O it will automatically generate the explicit imports, prompting you for the ambiguous imports.

like image 57
Jon Avatar answered Oct 24 '22 16:10

Jon


It will import only classes in java.awt, so you have to import java.awt.event too:

import java.awt.*
import java.awt.event.*;

Second method will probably load less classes, but won't save you much memory.

like image 38
Denis Tulskiy Avatar answered Oct 24 '22 14:10

Denis Tulskiy