您的位置:首页 > 编程语言 > Java开发

JAVA变量初始化及变量作用范围

2013-03-20 13:33 351 查看
我将两个示例合并成了一个,来显示JAVA变量作过的范围,

我想在这真正运用的时候,可能才能获得更深入的了解吧。

public class TestVar {
private int i = 100;
private static String name = "Learn Java fastly!";
{
int block = 10;
}
public int firstMethod() {
int j = 1;
System.out.println("in firstMethod, we can access  static name :" + name);
System.out.println("in firstMethod, i = " + i + ", j = " + j);
return 1;
}
public int secondMethod(float f) {
int j = 2;
System.out.println("in secondMethod, we can also access  static name :" + name);
System.out.println("in secondMethod, i = " + i + ", j = " + j + ", f = " + f);
return 2;
}
public static void main(String[] args) {
TestVar t = new TestVar();
//System.out.println("in mainMethod, we can access  var block :" + t.block);
t.firstMethod();
t.secondMethod(3);
}
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: