java多个和或用括号,方法内有多个打开和关闭的花括号。 - Java

public class MyTestClass {

public static void main(String[] args) {

new MyTestClass().myMethod();

}

public void myMethod(){

{

//do something

}

{

//do something

}

{

//do something

}

}//method close

}//class close

What is the benefit of doing this? I have seen this kind of code.

解决方案

It is not common practice to do this kind of thing, and I wouldn't do it normally.

Those inner blocks ( i.e. { ... } ) can serve a couple of purposes:

Blocks limit the scope of any variables declared within them; e.g.

public void foo() {

int i = 1;

{

int j = 2;

}

// Can't refer to the "j" declared here. But can declare a new one.

int j = 3;

}

However, I wouldn't recommend doing this. IMO, it's better to use different variable names OR refactor the code into smaller methods. Either way, most Java programmers would regard the { and } as annoying visual clutter.

Blocks can be used to attach labels.

HERE : {

...

break HERE; // breaks to the statement following the block

...

}

However, in practice you hardly ever see labelled break statements. And because they are so unusual, they tend to render the code less readable.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值