难道try的作用域结束后,在try里面定义的变量就失效了嘛?

解决方案 »

  1.   

    是这样的,try里定义的变量只在局部(花括号里)有效
      

  2.   

    那如果我把try里面的变量定义在外面,如下面:
    public class test implements Cloneable{
       int age=23;
    public static void main(String[] args){
    test kl=new test();
                       test kk;
    try{
    kk=(test)kl.clone();
    kk.age=20;
           //System.out.println(kk.age);
    }
    catch(Exception e){System.out.println(e);}
    System.out.println(kl.age);
    System.out.println(kk.age);
    }
    }
    为什么又不行呢???
      

  3.   

    你需要 test kk;
    改为 test kk=null;