-2

Instead of copying and pasting the same import statements in every single class file is there a way to put all of the imports you need on just one file and then call that file for example instead of pasting this every time:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

could you somehow put those statements in just one file and then import that instead ie.

import My_Imports;
Nicholas
  • 13
  • 1
  • 3
    I don't think that's possible. As your question is tagged with IntelliJ, did you have a look at [IntelliJ's auto import feature](https://www.jetbrains.com/help/idea/creating-and-optimizing-imports.html)? – Moritz Nov 18 '21 at 20:37
  • If you use an IDE, they automate some of that tedium. i.e. eclipse: Ctrl+Shift+O – Romain Hippeau Nov 18 '21 at 20:37
  • 3
    Why? Your IDE manages all this for you, you don't need to think about it. Besides, it's fairly uncommon for the same set of imports to be needed everywhere. – Jim Garrison Nov 18 '21 at 20:38
  • 2
    Does every single one of your classes need an IOException? Does this have anything to do with intellij-idea? I haven't written an import statement in idea in years. – matt Nov 18 '21 at 20:44
  • 4
    Having too many imports might also be a sign that your class is too big. Consider splitting the code in several classes if you find a meaningful way to do so – Arnaud Denoyelle Nov 18 '21 at 20:45

1 Answers1

2

I have never seen it done like that. I don't think it is possible but even if it was, I don't see that as very readable code. It doesn't tell me everything I need to know without me having to visit the package where all the imports are.

I believe the only ways to import packages are to use the very specifc naming conventions i.e.

import java.util.List;

or to import a whole package

import java.util.*;

Rhett Harrison
  • 430
  • 4
  • 19