Java static关键字的几种用法

来源:互联网 发布:软件著作权办理流程 编辑:程序博客网 时间:2024/05/16 14:17

static修饰属性

例如:

public static int sta=0;

static修饰方法

例如:

public static void printfMethod(){}

static代码块

static {}

static导入包

import static java.lang.System.out;

示例代码

import static java.lang.System.out;//静态导包public class HelloStatic {    public static int a=10;//静态属性    public static void method(){//静态方法//      System.out.println("this is static method");//没有静态导入之前        out.println("this is static method");//静态导入之后    }    static {//静态块//      System.out.println("this is block of static and static a="+a);//没有静态导入之前        out.println("this is block of static and static a="+a);//静态导入之后    }    public static void main(String[] args) {//静态方法        HelloStatic.method();    }}

输出结果

this is block of static and static a=10this is static method