JAVA变量初始化及变量作用范围
天飞 2013-03-20 13:33:00 浏览14 评论0java string static class void Access
摘要: 我将两个示例合并成了一个,来显示JAVA变量作过的范围, 我想在这真正运用的时候,可能才能获得更深入的了解吧。 public class TestVar { private int i = 100; private static String name = "Learn Java f...
我将两个示例合并成了一个,来显示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);
}
}
运行结果:
用云栖社区APP,舒服~
【云栖快讯】红轴机械键盘、无线鼠标等753个大奖,先到先得,云栖社区首届博主招募大赛9月21日-11月20日限时开启,为你再添一个高端技术交流场所 详情请点击 评论文章 (0) (0) (0)