日期:2014-05-20  浏览次数:20823 次

提问
class   Yan{
          static   int   a=1,b=2;
              b=a+2;
          public   static   void   main(String[]   args){
                         
                        System.out.println(a);
                        System.out.println(b);
            }
}为什么编译通不过,知道的请指点一下,谢谢!

------解决方案--------------------
b=a+2;运算要放到访法里
------解决方案--------------------
public class temp{
static int a=1, b=2;
{b=a+2;}
public static void main(String[] args){

System.out.println(a);
System.out.println(b);
}
}
------解决方案--------------------
谢了
------解决方案--------------------
这样做有什么作用吗?问什么结果还是 a=1, b=2 ????
------解决方案--------------------
public class temp{
static int a=1;


public static void main(String[] args){
int b=a+2;
System.out.println(a);
System.out.println(b);
}
}
这样b就等于3
------解决方案--------------------
运算要放到访法里
------解决方案--------------------
这样做有什么作用吗?
------解决方案--------------------
public class Test4 {
static int a=1;
static int b=2;
public static void main(String[] args){
b=a+2;
view(a);
view(b);
}
static void view(int s){
System.out.println(s);
}
}
------解决方案--------------------
b=a+2;


类里面不能有运算,想做就放到方法里面去调用