Is there an easy to use tool that OPs could use to find variable's whose class level definitions are Shadowed by local definitions of variables with the same name.
I see that problem a couple of times a week.

Do the standard IDEs have options that would warn about the condition?

When do you ever want to shadow a class level variable with a local definition?

Why isn't that a warning from the javac.exe program?

Recommended Answers

All 5 Replies

Do the standard IDEs have options that would warn about the condition?

Yes, Netbeans warns you of that.

When do you ever want to shadow a class level variable with a local definition?

No good case for it really comes to mind. It's just likely to cause confusion at the least and hard to spot bugs at worst.

I guess the one exception that is somewhat common is parameters of the same name in constructors, where the local variable is qualified with 'this'.

Why isn't that a warning from the javac.exe program?

No idea.

Yes, Eclipse does this - it has a number of variants on name shadowing that you can configure for warning or not.

> Is there an easy to use tool that OPs could use to find variable's whose class level definitions are Shadowed by local definitions of variables with the same name?

Not exactly beginner friendly but any serious development house at least has "FindBugs" or "PMD" running on their code bases to identify shadowing and many other serious bugs (e.g. synchronization).

... any serious development house at least has "FindBugs" or "PMD".

Do you have any views on how these two compare? Do you use only one (in which case, which?) or both?

They can be thought of as complementary tools as opposed to competitors. Findbugs is the easiest to get started with. Just install the relevant plugin for your IDE (the easiest way; of course you can also run it on a standalone Java project configured in a text editor from command line), right click on your project, click FindBugs and you are ready to go. PMD is a bit more complicated since it relies on rules and rule sets but if you don't mind the defaults, the setup is the same as that of Findbugs.

A note though; sometimes these plugins end up being pedantic, too pedantic. This is especially true for PMD. Plus, you might end up facing a couple of false positives here and there, for e.g. this one. I personally use both though I'm a bit more realistic on what I end up fixing.

Also, it's a bit surprising that the problems discussed in this thread for some reason isn't flagged by Findbugs/PMD though they give good suggestions nonetheless. ;-)

Also read: PMD v/s Findbugs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.