nding
nding

Reputation: 17

Using .*; after importing a class

I'm learning some java classes through the Java API and I'm just wondering when you should be using the .*; after each import. Here is what I have for the program so far.

import java.awt.GridLayout;
public class GridLayoutClass {
    public static void main(String[] args){

        GridLayout grid = new GridLayout(3, 4);
    }
}

Upvotes: 0

Views: 112

Answers (2)

Prateek
Prateek

Reputation: 1926

.* acts as a wild card. So, import java.awt.* would mean to include all the classes that are contained in awt package. As @Bill the Lizard♦ pointed out it is more towards development style to decide on if you want to specifically point out the classes you import or just put in a *. I personally consider pointing out the imported classes as better design decision so that you can keep track of classes you are working with and possibly reduce the probability of messing up with a class that was imported when you added .* but never intended to use.

Upvotes: 0

Bill the Lizard
Bill the Lizard

Reputation: 405795

If you need to import more than one class from a package you can use .* in your import statement like:

import java.awt.*;

This will import everything you need from the java.awt package.

It's really a matter of style, but some developers like to explicitly list out each class being imported instead of using the .* import shorthand. Some only use it when they're using a lot of classes from the same package. It's really up to you (and the other developers you're working with).

Upvotes: 2

Related Questions