class Count { |
public static int counter; |
static { //只运行一次 |
counter |
= 123 ; |
System.out.println( "Now in static block." ); |
|
|
} |
public void test(){ |
System.out.println( "test method==" +counter); |
} |
} |
public class Test { |
public static void main(String args[]) { |
System.out.println( "counter=" + Count.counter); |
new Count().test(); |
} |
} |
运行结果是: |
Now in static block. |
counter= 123 |
test method== 123 |
5 :静态 import |
当我们要获取一个随机数时,写法是: |
public class Test { |
public static void main(String[] args) { |
double randomNum = Math.random(); |
System.out.println( "the randomNum==" +randomNum); |
} |
} //源代码片段来自云代码http://yuncode.net |
|