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

final field assignment in a constructor violates Java Language Spec.

XMLWordPrintable

    • ppc
    • solaris_2.6

      The Java Language Specification(Gosling,Joy&Steele) page 146 says that
      a final field can only be initialized during the declaration time.

      "Any attempt to assign to a final field results in a compile-time error".

      But the following code compiles fine with sun javac

      public abstract class Font {
          public static final int PLAIN = java.awt.Font.PLAIN;
          public static final int BOLD = java.awt.Font.BOLD;
          public static final int ITALIC = java.awt.Font.ITALIC;

          public final String name;
          public final int style;
          public final int size;

          public static void init() {
              if (BitmapFont.initialize() != 0) {
                  // System.err.println("Bitmap fonts loaded.");
              }
          }

          public Font(String name, int style, int size, int ops) {
              this.name = name;
              this.style = style;
              this.size = size;
              this.fontOps = ops;
          }
      :
      :
      }

      This code does not compile with the Symantec java compiler.

      The Java Programming Language (Arnold & Gosling) page 111, however,
      specifies that it's ok to have the blank final declaration and initialize
      the final field in the constructor later.


            gbrachasunw Gilad Bracha (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: