å
³äºJavaä¸çstatic{}åï¼éè¿è°è¯ä»¥ä¸ä¸¤ä¸ªç¨åºå°±è½å¼æ¸
æ¥ç¨éï¼
1ã第ä¸ä¸ªå°ç¨åº
package com.babyDuncan.Sohu;
public class testStatic {
static
{
int x = 5;
}
static int x, y;
public static void main(String[] args) {
x--;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod()
{
y = x++ + ++x;
}
}
#该ç¨åºè¾åºä¸ºï¼3
åæå¦ä¸ï¼
æ§è¡mainä¸çx--ä¹åï¼xå¼ä¸º-1ï¼æ§è¡myMethodä¹åï¼x为1ï¼yå¼ä¸º0ï¼æ§è¡è¾åºè¯å¥è¡¨è¾¾å¼ï¼è¯¥è¡¨è¾¾å¼çå¼ä¸º1+0+2=3ï¼æ以è¾åºç»ææ¯3ã
2ã第äºä¸ªå°ç¨åº
package com.babyDuncan.Sohu;
public class testStatic2 {
/**
* å
³äºStatic{}åç解éï¼
* åªæ¯å¨æ§è¡mainä¹åæ§è¡çä¸äºè¯å¥èå·²ï¼å¹¶ä¸æ¯è¯´éé¢çåéå°±æ¯
* staticçï¼æ²¡ä»ä¹ç¹å«çã
* 临æ¶åéåªå¨staticè¿ä¸ªå¤§æ¬å·ä¸æç¨ã
**/
static {
int x = 8;
System.out.println("è¿æ¯staticï¼è¿æä¸ä¸ªåéx="+x);
}
static int x;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(x);
}
}
#è¾åºç»æ为ï¼
#è¿æ¯staticï¼è¿æä¸ä¸ªåéx=8
#0
温馨提示:答案为网友推荐,仅供参考