java命名空间冲突 [英] java namespace collisions

查看:44
本文介绍了java命名空间冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己经常犯以下错误.

I find myself frequently making the following error.

public class Foo{

  Bar bar;

  public void doSomething(){
    Bar bar = makeNewBar();
    // bla bla
  }

}

错误是我想设置 Foo 的栏,而不是本地栏.即,我应该说 bar = makeNewBar(),而不是 Bar bar = makeNewBar().是否有编译器警告标志或可以检测这样的命名空间冲突"的东西?如果没有,有什么好的方法可以避免这种情况(除了不犯错误,这显然是最好的选择 =p)?

where the error is that I want to set Foo's bar, not a local Bar. I.e, I should say bar = makeNewBar(), not Bar bar = makeNewBar(). Is there a compiler warning flag or something that can detect "namespace collisions" like this? If not, what's a good way to avoid this (short of not making the error, which is obviously the best option =p)?

推荐答案

这不是编译器的真正工作,但如果您隐藏变量,大多数 IDE 会以正确的设置警告您.

That's not really the job of the compiler but most IDE warn you with correct settings if you shadow a variable.

使用 Eclipse,转到 Preferences/Java/Compiler/Error/Warnings 并查找 Name shadowing and conflict.

Using Eclipse, go to Preferences / Java / Compiler / Error/Warnings and look for Name shadowing and conflicts.

这篇关于java命名空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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