同时,如果将 f 改成 static 数组的初始放在函数内部,也可以通过:
如下public class Test3 
{
  
    static int[] f;
   public static void main(String args[])

   
       f = new int[] {5,6};
   System.out.println(f[1] );       
    }}
以上是什么原因啊

解决方案 »

  1.   

    因为main函数是static 的,要访问实例变量必须通过此类的对象才行。public class Test3 
    {
      
       int[] f;
       public static void main(String args[])
       {
           Test3 t = new Test3();
     
           t.f = new int[] {5,6};
           System.out.println(t.f[1]);
        }
    }
      

  2.   

    如果楼主学过C的话,就知道当你declaring a variable, such as "int number",实际
    是一个省略形式,完整形式“static int number”,
    比方说你public class Test3 
    {
       int[] f;
       public static void main(String args[])

           f = new int[] {5,6};
       System.out.println(f[1] );
         
        }
    }
    这里的int【】f并不是你希望中declaring array,而是变成一个your own的符号语言。
    但是如果加上static之后,static int【】f才是你希望中declaring array
    大概是这个样子吧
      

  3.   

    public class Test3 
    {
      
      static int[] f = new int[] {3,6};
      // static int[] f;           定义和初始分开行不通,为什么?
      // f = new int[] {3,6};
      static int[] g ;
     
       public static void main(String args[])

       int[] t ;           // 定义和初始分开可以 为什么?
       t = new int[] {6,7};
           g = new int[] {67,89};
           System.out.println(t[0] );
       System.out.println(f[0] );
       System.out.println(g[0] );
        }}
      

  4.   

    f在Test3中是一个类成员,必须通过对象访问,main函数属于类对象,而不属于对象,因此必须在main中创建出一个Test3对象obj,然后调用obj.f[1]。
    不知道说得对不对。
      

  5.   

    static 修饰符 意味着这个变量在内存里分配了固定的存储空间,所以定义的时候必须要赋初值的。
      

  6.   

    回答楼上的问题
    java程序中大部分的语句都必须放在函数里,在类级别,只能有声明变量的语句,也可以在声明变量的同时进行初始化,但不能分开写