Java Import all classes from another package

Asked

Viewed 5,876 times

-4

I have 2 packages, I would like to import to package B all classes of package A.

1 answer

4


I would like to import into package B all classes of package A

It is impossible to import classes into another package. This does not exist.

It is only possible to import classes from a package to a class.

To import all classes from package A to class B, you can do it two ways: explicit import or implicit import.


Explicit Import

Explicit import is used when you import exactly that class of any package. For example:

import br.com.teste.controller.ClienteController;

Whereas ClienteController is a class.


Implicit Import

Implicit import is when you import all classes of some package into its class without declaring the import one by one.

Understand: You can import all classes of a package into your class using explicit import as well. Only it can end up being a bit of a hassle depending on the package size.

To perform an implicit import do so:

import br.com.teste.controller.*;

Thus all the classes of the package br.com.teste.controller shall be imported into their class.


Concrete Example

Let’s assume that the package br.com.teste.controller is composed of the classes:

  • ClienteController
  • UsuarioController
  • ProdutoController
  • PedidoController

Explicit import:

import br.com.teste.controller.ClienteController;
import br.com.teste.controller.UsuarioController;
import br.com.teste.controller.ProdutoController;
import br.com.teste.controller.PedidoController;

Implicit import:

import br.com.teste.controller.*;
  • Thank you for your reply c:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.