小example说明java的值传递和引用传递,基础变量和对象在内存里的区别,堆栈

本文介绍了一个简单的Java程序案例,展示了如何使用try-finally块来处理异常,并探讨了在finally块中修改对象状态的影响。通过一个具体的例子,我们创建了一个名为Test2333的类,该类包含两个整型变量x和y及其相应的getter和setter方法。另一个名为ExceptionTest的类中定义了一个名为txt的方法,此方法在try块内设置x和y的值,并在finally块中修改y的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ExceptionTest {

    public Test2333 txt(){
        Test2333 test = new Test2333();
        try {
            test.setX(1);
            test.setY(1);
            return test;
        }finally{

            test.setY(10);

        }
    }

    public class Test2333{
        int x;
        int y;
        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }
    }
 public static void main(String[] args){
     ExceptionTest exceptionTest = new ExceptionTest();
     Test2333 test = exceptionTest.txt();
     System.out.println(test.getX()+"~~~"+test.getY());
    }
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值