
4条回答 默认 最新
csdngxl 2021-12-02 09:12关注全局变量与方法内的局部变量重名时,方法内的局部变量会覆盖全局变量从而输出Hello,如需输出Hi,使用this关键字即可
public class Test2 { String str = new String("Hi!"); public void change (String str){ str = "Hello"; System.out.println(this.str); } public static void main(String[] args) { Test2 t2 = new Test2(); t2.change(t2.str); } }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用 5