kloddant
kloddant

Reputation: 35

How to import a package in java?

I'm trying to learn java and the awt framework from this tutorial: https://www3.ntu.edu.sg/home/ehchua/programming/java/j4a_gui.html, but I have hit a stumbling block: how do you import packages? For instance, here is what I don't want to do:

import java.awt.*  //pollutes global namespace and results in hard-to-trace class names

I also don't want to reference the java package every time I want to use one of the awt package's classes:

setLayout(new java.awt.FlowLayout()) //cumbersome and redundant

What I want to do is something like this:

from java import awt
setLayout(new awt.FlowLayout()) //I want to use awt here without saying java.awt

Is something like this possible in java, or does the language just not allow it?

I also don't know where setLayout comes from, but that is beside the point. I assume it is a static method of Frame that is being implicitly called?

Upvotes: 2

Views: 3928

Answers (4)

PrabaharanKathiresan
PrabaharanKathiresan

Reputation: 1129

In Java you can do import by

import java.awt.*; This will import all awt classes into your program either you are using them all or not but you import all awt classes in your program. In this way you are overloading your program by importing all awt classes.

Instead you can do what you want by specifying class name.. import java.awt.FlowLayout; This will import only the class file. This is standard and accepted way in java.

java static import import static java.lang.System.*; if you want to use static methods in the class you can use like this. This way you can avoid class name before static methods.

If you are going to work on awt packages.. I would suggest you to import by class names. Most of the awt components are refurbished in swing package. To avoid problems use class names with package and think use of swing components instead of awt.

Upvotes: 0

Jay Smith
Jay Smith

Reputation: 2480

No you can't split package names. You have two options:

  1. import java.awt.FlowLayot; This imports class FlowLayout of package java.awt

  2. import java.awt.*; This imports all classes of package java.awt.

Upvotes: 1

Ashwin Gupta
Ashwin Gupta

Reputation: 2197

You can import individual classes if you are worried about pollution.

Example

import java.awt.Rectangle;
import java.awt.Graphics2D;
...

I don't believe there is anyway to do exactly what you described.

Its also worth pointing out, you can do an import with a wildcard and clear up any ambiguous cases using the full package name.

import java.awt.*;
import opencv.*; //both awt and opencv have a Rectangle class

void foo() {
    java.awt.Rectangle r1 = new java.awt.Rectangle();
    opencv.Rectangle r2 = new opencv.Rectangle();
}

Also see this for a bit more info: https://stackoverflow.com/a/149282/4484072

@LewBloch brought up an important point, which is that there is no true hierarchy to java packages. So for example, java.awt.* will only include what is in that package, not other packages that start with java.awt such as java.awt.event. These names simply look as if they are related to you, but in reality are treated individually by the JVM

Upvotes: 3

Bor Laze
Bor Laze

Reputation: 2516

As far as I understand, you want to use in Java something like namespaces in C++.

Answer: NO, Java does not have such feature.
You can use import for whole package (import java.awt.*), import for concrete class (import java.awt.Rectangle) or you should declare full class name when use it (new java.awt.FlowLayout()).

Nothing else.

Upvotes: -1

Related Questions