HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Basics / Static import

Static Import Declarations in Java

The normal import declaration imports classes from packages, so that they can be used without package reference. Similarly the static import declaration imports static members from classes and allowing them to be used without class reference.

A static import declaration also comes in two flavors: single-static import and static-import-on-demand. A single-static import declaration imports one static member from a type. A static-import-on-demand declaration imports all static members of a type. The general syntax of static import declaration is as follows:

//Single-static-import declaration:

import static <<package name>>.<<type name>>.<<static member name>>;

//Static-import-on-demand declaration:

import static <<package name>>.<<type name>>.*;

Static import example

For example, You remember printing messages in the standard output using the System.out.println() method. System is a class in java.lang package that has a static variable named out. When you use System.out, you are referring to that static variable out of the System class. You can use a static import declaration to import the out static variable from the System class as follows:

import static java.lang.System.out;

You code can now use the name out to mean System.out in your program. The compiler will use the static import declaration to resolve the name out to System.out.

public class StaticImportTest {
        public static void main(String[] args) {
                out.println("Hello static import!");
        }
}

Static import rules

The following are some important rules about static import declaration.

1) If two static members with the same simple name are imported, one using single-static import declaration and other using static-import-on-demand declaration, the one imported using single-static import declaration takes precedence.

Suppose there are two classes, package1.Class1 and package2.Class2. Both classes have a static method called methodA. The following code will use package1.Class1.methodA() method because it is imported using the single-static import declaration:

import static package1.Class1.methodA; // Imports Class1.methodA() method
import static package2.Class2.*;  // Imports Class2.methodA() method too
 
public class Test {
        public static void main(String[] args) {
                methodA();   // Class1.methodA() will be called
        }
}

2) Using single-static-import declaration to import two static members with the same simple name is not allowed. The following static import declarations generate an error because both of them import the static member with the same simple name of methodA:

import static package1.Class1.methodA;
import static package1.Class2.methodA; // An error

3) If a static member is imported using a single-static import declaration and there exists a static member in the same class with the same name, the static member in the class is used.

// A.java
package package1;
 
public class A {
        public static void test() {
                System.out.println("package1.A.test()");
        }
}
 
// Test.java
package package2;
 
import static package1.A.test;
 
public class Test {
        public static void main(String[] args) {
                test(); // Will use package2.Test.test() method, not package1.A.test() method
        }
 
        public static void test() {
                System.out.println("package2.Test.test()");
        }
}

Output:

package2.Test.test()

It may seem that static imports help you use simple names of static members to make the program simpler to write and read. Sometimes static imports may introduce subtle bugs in your program, which may be hard to debug. You are advised not use static imports at all, or only in very rare circumstances.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Ramakrishna

    May 31, 2016

    Hello Lokesh,

    Excellent work…I feel.

    I have come across your blog while looking (&googling) System class on grepcode.
    And also noticed your article “…why finalize method should not be used….”; & “…best way to learn core java …”

    Very nice. I thin it needs few improvements (updating the main text of article with few more line taken from your valuable responses….particularly in your post “why not to use finalize method”),

    adding key points, in the top of the article (eg: ” ….static imports are not recommended to use) besides sating in conclusion at the end of the article

    • Lokesh Gupta

      June 1, 2016

      Thanks for the feedback. I will consider them adding in future.

Comments are closed on this article!

Search Tutorials

Java Basics

  • Java Introduction
  • Java Installation
  • Java Hello World
  • Java JDK, JRE and JVM
  • Java Classes and Objects
  • Java ClassPath
  • Java Operators
  • Java Data Types
  • Java Primitive Types
  • Java Variables
  • Java Comments
  • Java main() Method
  • Java Command Line Args
  • Java Wrapper Classes
  • Java Types of Statements
  • Java Block Statement
  • Java Pass-by-Value
  • Java System Properties
  • Java Static Import
  • Java hashCode() and equals()
  • Java Immutable Class
  • Java 32-bit vs 64-bit
  • Java java.exe vs javaw.exe
  • Java Generate Bytecode
  • Java Naming Conventions
  • Java Little-Endian vs Big-Endian

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces