New features of Java JDK5 - Overview and use static imports

Static Import Overview

 
  • Format: import static package name class name method name ...;.
  • You may be introduced directly into the process level

 

Precautions

 
  • The method must be static
  • If there are multiple static methods of the same name, it is easy to use do not know who ? This time you want to use, you must prefix. Thus, little significance, it is generally not, but to be able to read.

 

package cn.wen_02;

//import java.lang.Math;

import static java.lang.Math.abs;
import static java.lang.Math.pow;
import static java.lang.Math.max;

//错误
//import static java.util.ArrayList.add;

public class StaticImportDemo {
	public static void main(String[] args) {
	        //方式一
                // System.out.println(java.lang.Math.abs(-100));
		// System.out.println(java.lang.Math.pow(2, 3));
		// System.out.println(java.lang.Math.max(20, 30));
		// 太复杂,我们就引入到import
        
                //方式二
		// System.out.println(Math.abs(-100));
		// System.out.println(Math.pow(2, 3));
		// System.out.println(Math.max(20, 30));
		// 太复杂,有更简单

                //System.out.println(abs(-100));
		System.out.println(java.lang.Math.abs(-100));
		System.out.println(pow(2, 3));
		System.out.println(max(20, 30));
	}
	
	public static void abs(String s){
		System.out.println(s);
	}
}

 

 

Published 91 original articles · won praise 16 · views 1165

Guess you like

Origin blog.csdn.net/hewenqing1/article/details/104102625