-
Bug
-
Resolution: Won't Fix
-
P4
-
None
-
1.1.5
-
x86
-
windows_nt
Name: tb29552 Date: 09/11/98
/* The problem is that an expression of the form
*
* ClassName.staticFieldName.staticMethodName()
*
* is (sometimes?) not executed correctly.
*
*The problem can be reproduced with the following code:
*/
public class StaticBug
{
public
StaticBug()
{
System.err.println("Constructing an instance of class StaticBug");
}
public static void
main(String[] args)
{
SomeClass.someStaticField.someStaticMethod();
// Everything goes allright when the following two
// lines are used instead:
//
//StaticBug b = SomeClass.someStaticField;
//b.someStaticMethod();
}
public static void
someStaticMethod()
{
System.err.println("In StaticBug.someStaticMethod()");
}
}
class SomeClass
{
static
{
System.err.println("Initializing class SomeClass");
}
public static StaticBug someStaticField = new StaticBug();
}
/*
*
* The output should be:
*
* Initializing class SomeClass
* Constructing an instance of class StaticBug
* In StaticBug.someStaticMethod()
*
* but it is:
*
* In StaticBug.someStaticMethod()
*
* So class SomeClass is not initialized.
*
* According to the JLS (First printing, August 1996)
* section 15.11.4.1, the expression
* 'SomeClass.someStaticField' should be "evaluated, but
* the result is then discarded." To me, this implies
* that SomeClass is loaded and initialized, and that an
* instance of StaticBug is created.
*
* As shown above, if the single statement is split
* into two parts, everything goes as specified.
*
* I marked this bug as a compiler bug, but I'm not
* sure. Since all Java compiler/VM combinations I've
* tried behave the same, it may be a VM or VM spec bug.
*/
(Review ID: 37734)
======================================================================
- relates to
-
JDK-4069653 Invocation of static methods via a class instance
-
- Closed
-