zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

JAVA变量 作用 初始化 范围
2023-09-27 14:28:45 时间
我将两个示例合并成了一个,来显示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);

 运行结果: