Java变量范围 [英] Java variable scope

查看:127
本文介绍了Java变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当变量在本地范围和全局范围内初始化时,我们如何在不使用这个关键字的情况下使用全局范围?

when a variable is initialize both in local scope as well as global scope how can we use global scope without using this keyword in the same class?

推荐答案

class MyClass{
    int i;//1
    public void myMethod(){
        i = 10;//referring to 1    
    }

    public void myMethod(int i){//2
        i = 10;//referring to 2
        this.i = 10 //refering to 1    
    }    
}  

另见:

  • Shadowing Declarations
  • what-is-variable-shadowing-used-for-in-a-java-class

这篇关于Java变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆