62,614
社区成员
发帖
与我相关
我的任务
分享
public class classname()
{
static
{
//static直接跟{},{}里面写代码.
code......
}
}
class StaticTest {
public static int h = 100;
private int p;
static {
System.out.println("The static varaible is: " + h);
}
public static void incDisplay() {
System.out.println("Now, the static answer is: " + ++h);
}
public StaticTest() {
p = 10;
}
public void objectDisplay() {
System.out.println("The answer is: " + p);
}
}
public class Test {
public static void main(String[] args) {
StaticTest.incDisplay();
StaticTest.incDisplay();
StaticTest test = new StaticTest();
test.objectDisplay();
}
}
class A{
A(){
System.out.println("构造A");
}
{
System.out.println("构造前调用")
}
}
public class Test {
public static void main(String[] args) {
Test1 t1a = new Test1();
Test1 t1b = new Test1();
Test2 t2a = new Test2();
Test2 t2b = new Test2();
}
}
public class Test1 {
static {
System.out.println("I am a static block.");
}
}
public class Test2 {
{
System.out.println("I am not a static block.");
}
}