2

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?

kloddant
  • 35
  • 5
  • `from java import awt` is semantically identical to `import java.awt.;*` (except that it isn't valid syntax) and would have exactly the same 'namespace-polluting' effect. Unclear what you're asking, or why. Most IDEs will fix the imports for you automatically. – user207421 May 02 '17 at 05:16

4 Answers4

3

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

Community
  • 1
  • 1
Ashwin Gupta
  • 2,159
  • 9
  • 30
  • 60
  • This way is the correct way, @kloddant. "I also don't want to reference the java package every time I want to use one of the awt package's classes" is accomplished by a single-type import anyway. Packages aren't truly hierarchical; the entire package name is a namespace. It's like URLs, `http://testng.org/doc/documentation-main.html` won't work in your browser with just `doc/documentation-main.html` either. – Lew Bloch May 02 '17 at 05:03
  • @LewBloch indeed. Its important to point out the idea that they aren't hierarchical. Many people don't get that at first, mostly the fault of IDEs like eclipse that make it seem so. – Ashwin Gupta May 02 '17 at 05:05
  • This is horrible language design. I want to have some idea of where the classes in my code are coming from. – TheInitializer Aug 27 '20 at 20:01
1

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.

Jay Smith
  • 2,331
  • 3
  • 16
  • 27
0

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.

PrabaharanKathiresan
  • 1,099
  • 2
  • 17
  • 32
-1

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.

Bor Laze
  • 2,458
  • 12
  • 20