1

How can I import multiple packages in one java file. For example if I created a main method in one java file and packages in different. I want to import 3 packages in one line then how can I import all in one line?

Akshay Soma
  • 87
  • 2
  • 3
  • 9
  • I suggest you look for a tutorial on Java packages and Java imports. You seem to be fundamentally confused about the concepts. Maybe https://howtoprogramwithjava.com/java-package/ and https://howtoprogramwithjava.com/java-imports/ (I'm not recommending them, I just found them with Google). – ajb Sep 08 '16 at 04:10
  • import it like `import java.mypackage` – bananas Sep 08 '16 at 04:17

2 Answers2

5

In Java, you can import entire package using *. to import multiple packages. e.g. import com.package1.*; to import everything from package1. To import multiple packages, e.g. import com.package1.*; import com.package2.*; import com.package3.*;

Importing on a single line like import package1.*,package2.*,package3.*; is not possible in java but you can import entire directory if the packages are in the same directory as in import com.*;

  • 1
    Also using the wildcard `*` doesn't lead to unnecessary class loadings, as Java selectively chooses only the classes it needs in compile time. – Upulie Han May 14 '22 at 18:19
1

The wildcard * can import everything from a single package:

import junit.*;

Just be sure you don't clutter your local namespace with classes you are not using. If you're using a Java IDE, consider using the Organize Imports feature (ctrl-shift-o in Eclipse) instead.

Community
  • 1
  • 1
Nate Vaughan
  • 3,471
  • 4
  • 29
  • 47