How to use static imports in Java?

Posted in /  

How to use static imports in Java?
ramyashankar

Ramya Shankar
Last updated on November 21, 2024

    My mentor always told me that it is a bad practice to use System.out.print statement, and we should rather use log4j to write messages to log files. System.out prints messages to the console. However, while we do our debugging and testing, especially on IDE, it is easy to add System.out statements. Eclipse IDE has a shortcut – you type syso and do ctrl+enter, you will get the entire System.out.println for you!

    How to use static import in Java?

    The above way is just one way to type println statements quickly. If we import the class System.out, we need not type it each time we want a print statement. Importing a static class is the same as importing any other class. We have to add the static keyword. Here is a program that prints a message to a user based on some condition:

    String mornMsg = "Hi, Good morning to all!";
    String afterNoonMsg = "Good afternoon, did you have your lunch?";
    String eveMsg = "Good evening, would you like some tea/coffee?";
    String nightMsg = "I wish for you to have a good night and sweet dreams!";
    
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter the time of the day, \n0-Morning\n1-Afternoon\n2-Evening\n3-Night");
    
    int time = scan.nextInt();
    
    switch(time){
    case 0: System.out.println(mornMsg); break;
    case 1: System.out.println(afterNoonMsg); break;
    case 2: System.out.println(eveMsg); break;
    case 3: System.out.println(nightMsg); break;
    }
    }

    We have kept it simple so that it is easier to explain and understand. We have four messages and one of the four messages is displayed based on user input of numbers 0-3. Note that for demonstration, we have used many System.out.println statements. We will now add the import

    import static java.lang.System.out;

    and replace all the above System.out.println statements to out.println:

    case 0: out.println(mornMsg); break;
    case 1: out.println(afterNoonMsg); break;
    case 2: out.println(eveMsg); break;
    case 3: out.println(nightMsg); break;

    We also add the import static java.lang.System. in ; and now we can type:

    Scanner scan = new Scanner(in);

    The same can be done with System.err, too, although we have not used it in our program. Here is the complete program:

    String mornMsg = "Hi, Good morning to all!";
    String afterNoonMsg = "Good afternoon, did you have your lunch?";
    String eveMsg = "Good evening, would you like some tea/coffee?";
    String nightMsg = "I wish for you to have a good night and sweet dreams!";
    
    Scanner scan = new Scanner(in);
    
    out.println("Enter the time of the day, \n0-Morning\n1-Afternoon\n2-Evening\n3-Night");
    
    int time = scan.nextInt();
    
    switch(time){
    case 0: out.println(mornMsg); break;
    case 1: out.println(afterNoonMsg); break;
    case 2: out.println(eveMsg); break;
    case 3: out.println(nightMsg); break;
    }

    Conclusion

    We have seen how to import static classes and use static variables without the class name. You can do this for any class, even user-defined classes.

    People are also reading:

    Leave a Comment on this Post

    0 Comments