注意,STATIC的修饰的类变量和类方法(静态),不需要实例化一个类来访问,可以直接引用。
代码:
1 class StaticModifier{ 2 static int i = 10; 3 int j; 4 StaticModifier() { 5 j = 20; 6 } 7 public static int addUp(int x, int y) { 8 return x + y; 9 } 10 } 11 12 public class StaticModifierTest{ 13 public static void main(String args[]) { 14 int a = 9; 15 int b = 10; 16 System.out.println(" var of class i = " + StaticModifier.i); 17 StaticModifier s = new StaticModifier(); 18 System.out.println(" var of instant j = " + s.j); 19 int c = StaticModifier.addUp(a, b); 20 System.out.println("addUp() gives: " + c); 21 } 22 }
结果:
D:\javacode>javac StaticModifierTest.java
D:\javacode>java StaticModifierTest
var of class i = 10
var of instant j = 20
addUp() gives: 19