Bug 51075 - Compiler warning "is hiding a field" given for static inner class
Summary: Compiler warning "is hiding a field" given for static inner class
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal with 1 vote (vote)
Target Milestone: 3.0 M9   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 45779 49030 50253 53048 53827 55023 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-02-02 13:58 EST by Steve Harper CLA
Modified: 2004-11-22 06:45 EST (History)
4 users (show)

See Also:


Attachments
patch fixing the bug (2.41 KB, patch)
2004-04-21 07:37 EDT, David Van Hooste CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Steve Harper CLA 2004-02-02 13:58:27 EST
For this code:

public class CompilerTest
{
   private int hideIt;
   
   private static class InnerTest
   {
      private int hideIt;
   }
   
   public CompilerTest()
   {
      hideIt = 1;
   }
}

Under M6 get the compiler warning:
"The field CompilerTest.InnerTest.hideIt is hiding a field from type 
CompilerTest"

Lighten up - it's declared static.
My code uses static inner classes in several places that are now being flagged 
this way.
Comment 1 Philipe Mulet CLA 2004-02-13 07:11:04 EST
The language spec tolerates field hiding. As a courtesy we will warn the user 
if the optional diagnosis for field hiding is enabled.

You can turn this preference off in: 
Preferences>Java>Compiler>Advanced>Field declaration hides another field or 
variable.

Note that it is off by default.

When enabled, it doesn't distinguish static vs. non static, as all name 
collisions are just considered as poor style. As said before, even instance 
field collisions are allowed by language. Generally, naming conventions should
enforce distinguishing in between static vs. non-static (statics would be 
capitalized at least first letter).
Comment 2 Philipe Mulet CLA 2004-02-13 07:44:52 EST
*** Bug 45779 has been marked as a duplicate of this bug. ***
Comment 3 Philipe Mulet CLA 2004-02-13 07:47:18 EST
*** Bug 50253 has been marked as a duplicate of this bug. ***
Comment 4 Philipe Mulet CLA 2004-03-05 05:49:22 EST
*** Bug 53827 has been marked as a duplicate of this bug. ***
Comment 5 Philipe Mulet CLA 2004-03-05 05:50:01 EST
*** Bug 53048 has been marked as a duplicate of this bug. ***
Comment 6 Bogdan Stanescu CLA 2004-03-16 22:43:00 EST
I thought this warning was intended for contexts in which an expression could 
refer to either one of the variables. In the given example there is no such 
context.

> Generally, naming conventions should enforce distinguishing in between
> static vs. non-static (statics would be capitalized at least first letter).
You seem to be talking about static variables but the example is about static 
inner classes. The example respects the conventions, as far as I can tell.
Comment 7 quartz quartz CLA 2004-03-16 23:31:49 EST
Has been reported 5 times.
Comment 8 Philipe Mulet CLA 2004-03-17 09:52:11 EST
If you read the spec carefully, you'll notice that field hiding is legal. The 
example is legal code, and though it is legal code, we added a warning to help 
people resolve situations where variable names are confusing and may introduce 
nasty bugs down the road. The fact variables are field and/or static and/or in 
static context doesn't change this issue, the end program will be confusing, 
and could mislead a developper to use a slot in the wrong way.

This is only a warning, it is off by default. If you don't want field hiding 
diagnosis, then simply disable the warning, adding a special case when going 
through the static context would simply defeat the purpose of this enhanced 
diagnosis. 

I am still missing something obvious ?
Comment 9 Bogdan Stanescu CLA 2004-03-17 13:59:48 EST
Yes, the obvious thing is that in this case variable names cannot introduce any 
bugs.

All of us who reported this bug enabled the field hiding warning. Because we 
find it useful, but only in situations where it makes sense, not in this one.

Maybe an option "Include static inner class fields and variables hiding outer 
class non-static fields" can be added at the same level with "Include 
constructor or setter method parameters".
Comment 10 quartz quartz CLA 2004-03-18 10:23:57 EST
These variables names are in 2 different name domains: the static members and
the instance members.

Philippe tries to enforce detection of when an access to an enclosing instance
field is made from a static innerclass (which would be a compiler error because
there is no enclosing instance to a static inner class) BUT THAT static
innerclass just happen to have the same field name, and the coder would use it
by mistake.

I (we) say that coders often create utility static innerclasses that carry
similar values from enclosing instance (very frequent for non-anonymous
runnables, listeners, etc...) and the warning is not founded because we know
that we access a static-innerclass-local field.

For example, you want to define some treenode static innerclass to be used in a
tree class.
Tree and treenode both have a "children" and "size" variable. Because of that
warning, coders must figure out alternate names for these variable, like
"_children" and "_size". It is just plain annoying.

Beside, I would like to have field hiding as an error, not a warning, because it
is a major concern for avoiding bugs, just like I put "if(boo=true)" as an error
too. I just don't want sane code to show up as a problem.

Please do not leave on WONTFIX, it is sending a very negative message.
It is a good option, and we understand it could be done LATER, but not never.
Thanks.
Comment 11 Roel Spilker CLA 2004-03-18 11:41:30 EST
Maybe an option "Include static fields" could be added as well, since some of 
us already have an error set on "Non-static access to static member" 
and "Indirect access to static member". 

This way fields used by reflection, like serialVersionUID during serialization, 
could be named the same without any confusion about which field is being 
referred to.

Comment 12 Philipe Mulet CLA 2004-03-25 06:51:28 EST
*** Bug 55023 has been marked as a duplicate of this bug. ***
Comment 13 Philipe Mulet CLA 2004-03-25 06:52:10 EST
*** Bug 49030 has been marked as a duplicate of this bug. ***
Comment 14 Dani Megert CLA 2004-04-07 10:48:59 EDT
I agree that a warning is a good idea and that such code is often bad. However,
what confuses is the term "hides" which in programming has some meaning. I would
suggest some other terminology like "duplicate name" or "name already in used".

I am reopening this one since the number of dups really indicates that this is a
problem for users.
Comment 15 Philipe Mulet CLA 2004-04-07 12:41:25 EDT
We could indeed work the problem differently to rather mention a name collision 
as it is really what we are diagnosing.

Would it be an acceptable tradeoff to all ?

"The field CompilerTest.InnerTest.hideIt name is colliding with a field from 
type CompilerTest"
Comment 16 quartz quartz CLA 2004-04-07 13:00:20 EDT
as long as comment #9 is implemented.
Comment 17 Philipe Mulet CLA 2004-04-07 13:07:59 EDT
Comment #9 would not be implemented. The argument is that this warning would 
only blame the name, not mention hiding any longer. 
Comment 18 Philipe Mulet CLA 2004-04-07 13:10:15 EDT
Slight improvement:

The name of field {0}.{1} is already used by a field from type {2}. Though it 
is legal, it should be named differently to avoid confusion
Comment 19 Dani Megert CLA 2004-04-07 13:12:49 EDT
If we do so we must not forget to update the JDT UI preference page.
Comment 20 Roger Talkov CLA 2004-04-07 13:25:24 EDT
a field in a static inner class cannot hide an outer class field with the same 
name so there should be no warning.
To me a hide field warning is usefull for subclasses, having a subclass with a 
field that is a duplicate of one in the base class is almost always a mistake.
While an inner class having a field with the same name may not be. I have done 
this many times without inadvertantly hiding an outer class field

How about having a checkbox (or another option) for giving warnings on non-
static inner classes would be OK with me
Comment 21 Philipe Mulet CLA 2004-04-15 16:56:04 EDT
Ok, you convinced me. We will look into addressing this one for M9, 
with/without an extra option (if no extra option, then the offending scenario 
will no longer be reported).

Note that in case a static field is existing in the enclosing type, then this 
would still be reported as a potentially hiding issue.
Comment 22 David Van Hooste CLA 2004-04-21 07:37:47 EDT
Created attachment 9771 [details]
patch fixing the bug
Comment 23 David Van Hooste CLA 2004-04-21 07:41:40 EDT
The patch fixes the only case where the bug happened.

This case is the one described in the first message : when a field is in a 
static inner class with the same name of a field in an outer class. The 
compiler shouldn't warn us in this case.
Comment 24 Philipe Mulet CLA 2004-04-21 11:38:40 EDT
Thanks David. Patch integrated.
Fixed
Comment 25 Olivier Thomann CLA 2004-05-18 13:45:03 EDT
Verified in 200405180816
Comment 26 Maarten Bodewes CLA 2004-11-22 06:30:09 EST
This bug (or feature) still turns up; it seems the actual scope is not checked
with the submitted patch. It's a very nice feature to have (thanks), so please
look into it. Example code (anonymous inner class):

public class EclipseHidingBug {
  public void method() {
    int i = 0;
    Object b = new Object() {
      void m() {
        i = 1; // error, i not visible (correct)
        int i = 2; // warning - if enabled, hiding (incorrect)
      }
    }
  }
}

Comment 27 Philipe Mulet CLA 2004-11-22 06:43:30 EST
Are you saying the original issue is still there, or did you find another bug ?
Closing. Pls do not resurrect old fixed bugs, and rather log new ones for new
scenarii.
Comment 28 Philipe Mulet CLA 2004-11-22 06:43:50 EST
Retagging as verified
Comment 29 Philipe Mulet CLA 2004-11-22 06:45:18 EST
Entered bug 79168 for subsequent issue in comment 26.