Is there any way to generalize library imports into Java?

Asked

Viewed 115 times

10

I created inside the same package called Banco, two classes in Java: ContaSalario and ContaPoupanca. When I’m going to import these two classes into my main class, I do:

import Banco.ContaSalario;    
import Banco.ContaPoupanca;

Is there any way to import all classes from the same package with a single call? For example:

import Banco.all;

3 answers

11


It’s possible, just do:

import Banco.*

That will import all the package class Banco.

Note however that according to Javabeans, packages should have the first minuscule letter, which is precisely to differentiate from classes. Then the most appropriate thing would be to change your Banco for banco. Your code would look like this:

import banco.*

5

Yeah, you can do that:

import Banco.*

This form imports all existing classes in the same package.

Documentation.

5

Import like this:

import Banco.*

Just put . * after the last package you want to import

  • 1

    It is worth noting two things here: this is the first time I see two answers being posted in the same exact second :) The other thing to note is that there is no package Conta within the package Banco :(

  • no problem, just put the "." + * after the last package you want to import

Browser other questions tagged

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