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

2.9: Clarify instance initialization methods



    • Bug
    • Status: Closed
    • P4
    • Resolution: Fixed
    • 8
    • 9
    • specification
    • vm


      (First, divide 2.9 into 2.9.1 Instance Initialization Methods, 2.9.2 Class Initialization Methods, and 2.9.3 Signature Polymorphic Methods.)

      An instance initialization method only makes sense for a class, so <init> in an interface is not an instance initialization method. Historically in an interface:
      - An abstract <init> was ignored by HotSpot if void, but caused CFE if non-void (since JDK 6 at least).
      - A non-abstract <init> was illegal prior to SE 8 simply because it was non-abstract. Since SE 8 permits non-abstract methods in interfaces, HotSpot since JDK 8 has treated such an <init> like any other default or static method in an interface. (For example, HotSpot in JDK 8 can throw VerifyError if an interface's <init> does not invoke a super-<init> method.)

      Regardless of the history that allowed abstract void <init>, the right thing to do is reject all <init> methods in interfaces with CFE.

      2.9.1 should say:
      - A class has zero or more instance initialization methods, each typically corresponding to a constructor written in the Java programming language.
      - A method is an _instance initialization method_ if all of the following are true:
        - It is defined in a class (not an interface).
        - It has the special name <init>. (Because the name <init> is not a valid identifier in the Java programming language, it cannot be used directly in a Java program.)
        - It is void.
      - In a class, non-void methods named <init> are not instance initialization methods. They cause CFE in HotSpot (since JDK 6 at least), so should be prohibited in 4.6 on the basis of their method descriptor. Such a method could not be invoked by a JVM instruction in any case (4.4.2, 4.9.2).
      - An interface has no instance initialization methods. Any method named <init> in an interface should be prohibited in 4.6 on the basis of an unsuitable method name.

      Both the declaration and use of an instance initialization method are constrained by the Java Virtual Machine. For the declaration, the method's access_flags item and the code array of the method's Code attribute are constrained (4.6, 4.9.2). For the use, an instance initialization method may be invoked only by the invokespecial instruction on uninitialized class instances (§invokespecial).


        Issue Links



              abuckley Alex Buckley
              abuckley Alex Buckley
              0 Vote for this issue
              4 Start watching this issue