Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6504504

Compiler error of non-static variable used in static context

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P4
    • None
    • 6
    • tools
    • x86
    • windows_xp

    Description

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

      NOTE: The web page did not have a selection for the Java 1.6.0 release, so I selected the beta 2 option.

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      The Java 1.6.0 compiler appears to be incorrectly indicating that a non-static variable (outer class's 'this' variable) is being used in a static context.

      This code compiles without error with Java 1.5.0_10 compiler.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1) Save the provided source code to appropriate .java file.
      2) Compile using Java 1.6.0 javac.
      3) See compiler error related to static use of non-static variable 'this'

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Code compiles with no errors. The JDK 1.5.0_10 release does compile this code without any errors.
      ACTUAL -
      Java 1.6 release compiler reports errors.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      d:\java\jdk1.6.0\bin\javac -cp . TestJava6.java
      TestJava6.java:13: non-static variable this cannot be referenced from a static context
                          Saver saver = new DefaultSaver();
                                                    ^
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      public abstract class TestJava6 {

          private static TestJava6 INSTANCE;

          public abstract Saver newSaver();

          public static TestJava6 get() {
              if (INSTANCE == null) {
                  INSTANCE = new TestJava6() {

                      public Saver newSaver() {
                          Saver saver = new DefaultSaver();
                          return saver;
                      }
                  };
              }
              return INSTANCE;
          }

          public static void set(TestJava6 factory) {
              INSTANCE = factory;
          }

          private class DefaultSaver implements Saver {
              public void save(String fileName) {
              }
          }

          public interface Saver {
              public void save(String fileName);
          }
      }


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None if it is required that the DefaultSaver nested class is required to be a non-static class.

      Making the DefaultSaver class static will result in a successful compilation. However, that doesn't resolved what appears to be an incorrect compilation error from the Java 1.6 compiler.

      Attachments

        Activity

          People

            ahe Peter Ahe
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: