Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested packages in Java

Firstly I want to create a custom user library with the following structure:

src:

  • LibA.pack1
    • ClassName0.java
  • LibA.pack2
    • ClassName1.java

I have no problem with this one. Later I want to import this library into the other project and call

import LibA.*;

(to use both classes of pack1 and pack2), which will fail as it requires full name, i.e.

import LibA.pack1;

How can I import the whole library at once to be able to use both classes of pack1 and pack2?

P.s. It's definitely not called "nested packages" but I have no idea how to call this. P.p.s. I'm using Eclipse if it matters.

Thanks in advance:)

like image 288
Denis Korekov Avatar asked Oct 22 '14 13:10

Denis Korekov


People also ask

What is a nested class in Java?

In java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation, and create more readable and maintainable code.

What is a package class in Java?

Package class. A java package is a group of similar types of classes, interfaces and sub-packages. Package in java can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning ...

How to access package from another package in Java?

How to access package from another package? 1 Using packagename.* If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. ... 2 Using packagename.classname If you import package.classname then only declared class of this package will be accessible. ... 3 Using fully qualified name

What are the built-in packages in Java?

There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages.


1 Answers

You can't, since there is no such thing as nested packages in java. You must import both packages explicitly.

import LibA.pack1.*;
import LibA.pack2.*;

LibA.pack1 is not related in any way to LibA.pack2, and both of them have no relation to LibA package, so if LibA has additional classes you wish to import, you'll need a 3rd import :

import LibA.*;

Apparent Hierarchies of Packages

At first, packages appear to be hierarchical, but they are not. For example, the Java API includes a java.awt package, a java.awt.color package, a java.awt.font package, and many others that begin with java.awt. However, the java.awt.color package, the java.awt.font package, and other java.awt.xxxx packages are not included in the java.awt package. The prefix java.awt (the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.

Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color, java.awt.font, or any other java.awt.xxxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt, you must import both packages with all their files:

import java.awt.*;
import java.awt.color.*;
like image 172
Eran Avatar answered Oct 16 '22 06:10

Eran