2。局部变量设为static类型的话,就不是局部变量了,因为static类型的变量的生命期是和类相同的。只所以用局部变量是考虑到在方法调用完成后,局部变量也会自动消失,这样就没有必要用static类型了。

解决方案 »

  1.   

    1.如果值为整数,则类型为int.所以上面的f1=-1,f2=0x0123可以通过编译哦!
      

  2.   

    xuancao(飞云)说的有道理!谢谢了!
    不过,局部变量设置为static类型,解释得我还是有点不太明白
    llsoft(天堂鱼),尽管设置成static型,不是局部变量了,但是如果我一定要这么用,为什么编译不过去呢?
      

  3.   

    static型的变量是所有这个类以及其子类共享的变量,是全局变量,没有特殊原因,一般不以采用
      

  4.   

    有时候不要问为什么,用就好:)为什么int表示整形阿?不用知道:)
      

  5.   

    static 是在堆里面分配的,是全局的,
    而局部变量一般是在栈里面分配的,除非你自己申请在堆里面分配。
    函数执行完,栈中的东西就没了。
      

  6.   

    1.) 编译器自动进行转化 - 
    double - 可以接受 byte, int, float, long  等类的初始值。
    float - byte, int etc.float f = 0;   // OK. - 0 is an int.
    float f = 0.0; // Error! - 0.0 is a double!
    float f = 0L;  // Error! - 0L is a long!
    double d = 0L; // OK2.) Do not mix the 'static' in C funtion with 'static' in Java. 
    static - in Java, the memeber belong to the class, not the object.
    static - in C's funtion, remember the value after function called.If you want to use something like 'static' in C, the best way to implement this is to use an object variable.
      

  7.   

    1.xuancao(飞云)说的正确。我改为 float f1=-1.0;后出错,改为float f1=1正确,原因在于:float f1只是声明变量,=为赋值,而-1、1、0x123等为整数故正确,整数向浮点转换不需强制转换的,因此,float f1=(float)1.0;也是正确的。2.因为static在类初始化时装载,为所有该类对象所共有,而非static的为相应的对象所有,在构造对象时在对象中产生,因此不能用作static,或者你在static中声明static的变量,当然static的函数中是可以有非static变量的,你看main函数(自己想一想就明白了)。
      

  8.   

    我想再问一下,在内部类中不能声明静态成员,但是,如果在静态内部类中是不是可以声明静态成员了呢?我编译运行了一下,没有出错。下面是例子:
    public class Outer
    {
      public static class Inner
            {
    static int i=5;
    }
    }//这是一个含有静态成员的内部类
    public class Test
    {
    public static void main(String[]  args)
    {
    Outer.Inner oi=new Outer.Inner();
    }
    }//测试这个类
    请问这个怎么解释?