我将两个示例合并成了一个,来显示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); } }
运行结果:
时间: 2024-09-19 08:55:41