---
title: "static import"
author: "yangyi"
date: "2011-05-22T10:51:39Z"
tags: []
categories: ["编程语言"]
slug: "static-import"
draft: false
lastmod: "2011-05-22T10:53:03Z"
id: 1054224
url: "https://yangyi.iteye.com/blog/1054224"
---

Static import支持略去类型名的public static字段使用

如：
import static java.lang.Math.PI;
import static java.lang.Math.pow;
或全部:
import static java.lang.Math.*;

例子：

```java
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(5,2)) + "sq. cm");
    }
}
```

简化为：

```java
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(5,2)) + "sq. cm");
    }
}
```


一些简单的语法糖，好像没什么大用