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

[local-methods] Javac should allow local methods above the chained constructor call this()/super()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • repo-amber
    • repo-amber
    • tools
    • Fix Understood
    • generic
    • generic

      Given:
      public class X {
        private static int get42() {
            return 42;
        }
         X() {
             this(get42());
         }
         X(int x) {
         }
      }

      The finder recommends that get42() can be local to X()

      If you move it in as a local method, it has to lose its private and static modifiers and the code would look like
      (the only modifiers accepted in a local method is final)

      public class X {
         X() {
             int get42() {
                 return 42;
             }
             this(get42());
         }
         X(int x) {
         }
      }

      This presently triggers: X.java:8: error: call to this must be first statement in constructor.

      Javac should allow local methods above this()/super() call, implicitly forcing them to be static.

            vromero Vicente Arturo Romero Zaldivar
            sadayapalam Srikanth Adayapalam (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: