`
JinLoveJava
  • 浏览: 21648 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

变量作用域

阅读更多
package lianxii;

public class Variable {

    private int i = 1; //也可int i;

    public void method1() {

        int i = 4, j = 5;
        this.i = i + j;
        System.out.println(this.i);//使用当前定义的int i=1
        method2(2);
    }

    public void method2(int i) {

        int j = 8;
        this.i = i + j;
        System.out.println(this.i);//也可以String d=i+j,System.out.println(d);
    }

    public static void main(String[] args) {

        Variable t = new Variable();
        t.method1();

    }
}
输出结果:9 10

注:局部变量的作用域就是它所在的方法或语句块
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics