Search code examples
javaintellij-ideajava-17

How can I make imports less repetitive in Java


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;

Solution

  • 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.*;