不要上机,先猜猜输出什么?public class Test3 {

String str = "";

public static void main(String[] args) {
String test = new Test3().method(1);
System.out.println(test);
}

private String method(int a){
try {
if(a == 1){
throw new Exception();
}
str +="111";
} catch (Exception e) {
str +="222";
return str;
}finally{
str += "333";
}
str += "444";
return str;
}
}

解决方案 »

  1.   

    catch里return了会怎么样?
    str="444"???
      

  2.   

    a==1
    所以执行
    throw new Exception();
    异常被捕获
    str +="222";
    return str;
    执行return 前要先执行finally
    所以执行str += "333";
    结果是222333
    我没有运行过哦,错了谅解
      

  3.   

    执行try-catch后执行finally是正常的途径,但是在函数中不论在哪只要遇到return就必然将函数返回,这在很多JAVA书上都写的有,可我的疑问是,为什么定义了new Test()后可以直接调用私有变量啊?谁解答一下?
      

  4.   

    public class Test01 {
        
        String str = "";
        int i=0;
        public static void main(String[] args) {
           // String test = new Test01().method(1);
          //  System.out.println(test);
    System.out.println(new Test01().i);

        }
        
        private String method(int a){
            try {
                if(a == 1){
                    throw new Exception();
                }
                str +="111";
            } catch (Exception e) {
                str =str+"222";
    i++;
    System.out.println(str);
                return str;
            }finally{
    System.out.println("有没有执行");
                str =str+ "333";
    i++;
    System.out.println(str);
    System.out.println("有没有执行");
            }
            str += "444";
            return str;
        }
    }
      

  5.   

    public static void main(String[] args) 这个也是它本身类的方法呀...
    private 是可以在本类的其他方法中调用的嘛
      

  6.   

    那这样呢, 应该输出什么
    package test;
    public class Test3 {
        
        String str = "";
        
        public static void main(String[] args) {
            String test = new Test3().method(1);
            System.out.println(test);
            String test1 = new Test3().method(0);
            System.out.println(test1);
        }
        
        private String method(int a){
            try {
                if(a == 1){
                    throw new Exception();
                }
                return str +"111";
            } catch (Exception e) {
                str +="222";
                return str + "444";
            }finally{
                str += "333";
                return str + "555";
            }
        }
    }
      

  7.   


    public class Test01 {
        StringBuffer s=new StringBuffer("");
        public static void main(String[] args) {
    System.out.println(new Test01().method1(1));
        }
        private StringBuffer method1(int a){
            try {
                if(a == 1){
                    throw new Exception();
                }
    s.append("1111111111");
            } catch (Exception e) {
               // str ="222";
       s.append("aaaa");
                return s;
            }finally{
                s.append("333");
            }
              s.append("444");
            return s;
        }
    }
    经过测试,勉强可以理解了.
    当然执行到return str;的时候,这时候已经复制了这个引用了.所以返回的是"222"这个对像的地址,然后str重新指向了新的对像.可以比较一下边的结果.它返回的是aaaa而不是那个99999999public class Test01 {
        StringBuffer s=new StringBuffer("");
        public static void main(String[] args) {
    System.out.println(new Test01().method1(1));
        }
        private StringBuffer method1(int a){
            try {
                if(a == 1){
                    throw new Exception();
                }
    s.append("1111111111");
            } catch (Exception e) {
               // str ="222";
       s.append("aaaa");
                return s;
            }finally{
                s=new StringBuffer("99999999999999");
            }
              s.append("444");
            return s;
        }
    }楼主的代码改一下应该就可以看出不同的地方:public class Test3 {
        
        String str = "";
        
        public static void main(String[] args) { 
            Test3 t = new Test3();
            String test = t.method(1);
            System.out.println(test);
            System.out.println(t.str);//这个就应该是222333
        }
        
        private String method(int a){
            try {
                if(a == 1){
                    throw new Exception();
                }
                str +="111";
            } catch (Exception e) {
                str +="222";
                return str;
            }finally{
                str += "333";
            }
            str += "444";
            return str;
        }
    }
      

  8.   


    根据这个代码的结果发现.总是返回最后一个return 滴~~.
      

  9.   

    14楼OK,哈
    其实String test = new Test3().method(1); 中的test并非是Test3的对象中全局变脸str的值如果写成Test3 t = new Test3();
    String test = t.method(1);
    System.out.println(t.str);//这里输出的才是全局str,(*^__^*) 嘻嘻……