---
title: "Static imports in java"  
description: "Static imports in java"  
author: "Anonymous User"  
published: 2015-07-13  
updated: 2015-07-13  
canonical: https://www.mindstick.com/forum/23334/static-imports-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Static imports in java

What is [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) imports? Why it is used,what are [advantages and disadvantages](https://www.mindstick.com/forum/157827/what-is-a-computer-network-and-what-are-the-primary-advantages-and-disadvantages-of-using-one).

## Replies

### Reply by Anonymous User

It is a feature in the Java that allows members (fields and methods) defined in a class as public static to be used in Java code without specifying the class in which the field is defined.\
The feature provides a typesafe mechanism to include constants into code without having to reference the class that originally defined the field. It also helps to deprecate the practice of creating a constant interface: An interface that only defines constants then writing a class implementing that interface, which is considered an inappropriate use of interfaces.\
The mechanism can be used to reference individual members of a class:

```
import static java.lang.Math.PI;import static java.lang.Math.pow;
```

\
or all the static members of a class:

```
import static java.lang.Math.*;
```

\
For example, this class:

```
public class HelloWorld {    public static void main(String[] args) {        System.out.println("Hello World!");        System.out.println("Considering a circle with a diameter of 5 cm, it has:");        System.out.println("A circumference of " + (Math.PI * 5) + " cm");        System.out.println("And an area of " + (Math.PI * Math.pow(2.5,2)) + " sq. cm");    }}
```

Can instead be written as:\

```
import static java.lang.Math.*;import static java.lang.System.out;public class HelloWorld {    public static void main(String[] args) {        out.println("Hello World!");        out.println("Considering a circle with a diameter of 5 cm, it has:");        out.println("A circumference of " + (PI * 5) + " cm");        out.println("And an area of " + (PI * pow(2.5,2)) + " sq. cm");    }}
```

\
\
**Disadvantage:**\
**Amiguity:**It may result in error if same static member is imported from 2 different classes. As compiler will not be able to determine which one to use in absence of class name qualification. As Exp. If we have this kind of static import

```
import static java.lang.Integer.*;import static java.lang.Long.*;
```

In that case if we try to access MAX_VALUE field compiler will give "The field MAX_VALUE is ambiguous" error as field MAX_VALUE is in both INTEGER AND LONG classes


---

Original Source: https://www.mindstick.com/forum/23334/static-imports-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
