Class Test{
   String str1[] = new String[3];
   str1[1] = "CSDN";    //此处会出错,为什么呢?
   String str2[] = null;
   str2 = new String[10];    //此处会出错,为什呢?
   System.out.println();     //此处也会出错,为什么呢?
  //但如果改成以下定义这不会
 
  {
    String str3[] = new String [3]; 
    str3[1] = "java";
    String str4 = null;
    str4 = new String[4];
    System.out.println();           
  }
}
麻烦大家,我想知道其中原理!

解决方案 »

  1.   

    我修改了一下你的代码,运行了一下。class Test { //此处class应该全部小写,你的class中c大写了。  
    public void a() { //加了方法声明
    String str1[] = new String[3];
    str1[1] = "CSDN"; // 此处没出错;
    String str2[] = null;
    str2 = new String[10]; // 此处没出错;
    System.out.println(); // 此处没出错;
    } public void b() { //加了方法声明
    String str3[] = new String[3];
    str3[1] = "java";
    String str4 = null;
    //str4 = new String[4];  // 此处错了,因为str4声明不是数组类型,但是你却赋值为数组。
    System.out.println();
    }

    public static void main(String[] args){  //我加了一个main方法测试
    Test t = new Test();
    t.a();
    t.b();
    }
    }