-
Enhancement
-
Resolution: Fixed
-
P5
-
1.4.1, 5.0
-
b86
-
generic, x86
-
generic, windows_xp
-
Not verified
Name: rl43681 Date: 06/17/2003
A DESCRIPTION OF THE REQUEST :
The compiler allows static methods to be accessed as if they are instance methods. This can be a source of confusion when reading code.
It would be clearer if all calls to static methods had to be prefixed with the class name - but forcing this would break a lot of existing code.
It would be nice if the compiler had an option to warn when a static method is called without the class name being prefixed.
JUSTIFICATION :
To enhance the readabiloty of code (feel free to argue that it'll make the code harder to read - but hey that is why it is an option).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The compiler emit a warning (if the user specifies the flag) when a static method is called without being prefixed by the class name.
---------- BEGIN SOURCE ----------
public class X {
public static void main(final String[] argv) {
X x = new X();
x.foo(); // warning - reference not a class
X.foo(); // OK
}
public static void foo() {}
void bar() {
foo(); // warning - no class prefix
X.foo(); // OK
}
}
---------- END SOURCE ----------
(Review ID: 185711)
======================================================================
A DESCRIPTION OF THE REQUEST :
The compiler allows static methods to be accessed as if they are instance methods. This can be a source of confusion when reading code.
It would be clearer if all calls to static methods had to be prefixed with the class name - but forcing this would break a lot of existing code.
It would be nice if the compiler had an option to warn when a static method is called without the class name being prefixed.
JUSTIFICATION :
To enhance the readabiloty of code (feel free to argue that it'll make the code harder to read - but hey that is why it is an option).
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The compiler emit a warning (if the user specifies the flag) when a static method is called without being prefixed by the class name.
---------- BEGIN SOURCE ----------
public class X {
public static void main(final String[] argv) {
X x = new X();
x.foo(); // warning - reference not a class
X.foo(); // OK
}
public static void foo() {}
void bar() {
foo(); // warning - no class prefix
X.foo(); // OK
}
}
---------- END SOURCE ----------
(Review ID: 185711)
======================================================================
- duplicates
-
JDK-6177893 Guard against the invocation of static methods via instances (-Xlint)
-
- Closed
-
- relates to
-
JDK-6929544 langtools source code uses statics qualified by instance variables
-
- Closed
-
-
JDK-8024820 need annotation that causes compiler to complain when invoking static methods through instance variables
-
- Closed
-
-
JDK-6929156 need new lint options for compiler
-
- Closed
-