public class TestReturn {   
    public static void main(String[] args){   
        testNum();   
        try{   
            int a = 5;   
        }catch(Exception e){   
            e.printStackTrace();   
        }   
        a = 6;//这样写为什么会报错.在前面不是定义了a值了吗? 报错说没有声明变量   
           
    }   
    public static void testNum(){   
        int a = 5;   
        a = 6;   
        System.out.println(a);   
    }   
}  

解决方案 »

  1.   

    int a = 5; 是声明在try catch段里的局部变量。catch完结后 生命周期结束。
      

  2.   

    a变量在try{}里面
    所以当出了}时就被清空了
      

  3.   

    你定义的a的值是在子程序中,所以只对子程序有效,在子程序外面没有定义a所以直接给a赋值是不对的
      

  4.   


    public class TestReturn {   
        public static void main(String[] args){   
            int a ; 
            testNum();   
            try{   
                  a=5;
            }catch(Exception e){   
                e.printStackTrace();   
            }   
            a = 6;  
               
        }   
        public static void testNum(){   
            int a = 5;   
            a = 6;   
            System.out.println(a);   
        }   
    }  这样写就没错了,你要分清实例变量跟局部变量,看一下书,打一下代码就可以分清了。
      

  5.   

     int a ; 
    try{   
                int a = 5;   
            }catch(Exception e){   
                e.printStackTrace();   
            }   
            a = 6;//这样写为什么会报错.在前面不是定义了a值了吗? 报错说没有声明变量   注意  a的范围问题
      

  6.   

    int a = 5; 是声明在try catch段里的局部变量。catch完结后 生命周期结束。  
     
      

  7.   

    1楼正解 Java程序所有的变量的生存周期是在一对大括号内 
      

  8.   

    把子int a=5;定义为全局变量
      

  9.   

    int a = 5;  是局部变量,不是全局变量
      

  10.   

    try块中的int a =5;是在try中的,是局部变量,运行完后就消失了,所以后面的a=6;就等于没有定义a的类型。就会报错了。
      

  11.   

    a是局部变量
    要在TRY语句块外定义才行。可以在语句块内赋值
      

  12.   

    public class TestReturn {   
        public static void main(String[] args){   
           
            testNum();   
            try{   
                int a=5;
                  a = 6;          }catch(Exception e){   
                e.printStackTrace();   
            }   
                      
        }   
        public static void testNum(){   
            int a = 5;   
            a = 6;   
            System.out.println(a);   
        }   
    }  
    try{}里的为局部变量
    public class TestReturn {   
        public static void main(String[] args){   
            int a ; 
            testNum();   
            try{   
                  a=5;
            }catch(Exception e){   
                e.printStackTrace();   
            }   
            a = 6;            
        }   
        public static void testNum(){   
            int a = 5;   
            a = 6;   
            System.out.println(a);   
        }   
    }
     
      

  13.   

    全局变量与局部变量.
    用高级点的开发工具自动会提示这种错误,如:eclipse
      

  14.   

    int a 申明在try里面就成了局部变量,只能在try catch里面使用.
      

  15.   

    因为int a 应该声明在try块的外面,比如
    int a = -1;
    try{
       xxx...
    }catch(Exception e){
       e.printStackTrace();
    }
    a = 6;
      

  16.   

    1楼的是正解。。
    int a = 5;你这句话放在try里面,相当于是一个局部变量。。
    只要把这个拿到try外面就OK了   
      

  17.   

    变量超出范围,int a 在这里是区域变量.
      

  18.   

    a定义在try的{}中,只在其中有效,出去了就没了