2004-08-18

Jikes bug #3947

I haven't had time to do anything with Jikes for months now, but I finally got round to fixing bug #3947 last night. That's the one about whether we should warn if a local in a static method has the same name as an instance variable:


class C {
int i;
static void m() {
int i = 0; // Is this worth warning about?
}
}


One argument says that we shouldn't warn, because the instance variable isn't accessible from the static method.

Another argument says that we should warn, because it's confusing to have a local with the same name as an instance variable regardless of what kind of method it's in.

The best argument I came up with in favor of not warning is that it's quite common practice to write static methods that return an instance that they construct after preparing the constructor arguments in local variables. The usual idiom for constructor arguments is that they have the same names as the instance variables they correspond to, so it's likely that they'll be the same names used in the static method. Something like:


class C {
int i;
private C(int i) {
this.i = i;
}
static C fromString(String s) {
int i = Integer.parseInt(s);
return new C(i);
}
}


I also looked at what g++ does with equivalent C++, and found that it doesn't warn.

As of a few minutes ago, when I had chance to run the regression tests and found that the change doesn't break anything, CVS Jikes doesn't warn either.