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

Why private access specifier is not allowed for static methods in interfaces as we can access it from other static methods of that interface

XMLWordPrintable

      FULL PRODUCT VERSION :
      Windows :
      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

      Ubuntu : OpenJDK 8

      ADDITIONAL OS VERSION INFORMATION :
      Tested under Windows 7 as well as Ubuntu 16.04

      A DESCRIPTION OF THE PROBLEM :
      Why private access specifier is not allowed for static methods in interfaces as we can access it from other static methods of that interface ??

      PFB example:

      /**
      * Tested under Java 8 as well as OpenJDK 8
      */

      interface TestInterface
      {
         /*
          * why private access specifier is not allowed for method1 as we can call
          * method1()
          * from method2() irrespective of it's access specifier
          */

         // private static void method1()
         static void method1()
         {
            System.out.println("Inside method method1()");
         }

         static void method2()
         {
            System.out.println("Inside method method2(). Calling method method1()");
            method1();
         }
      }

      public class Java8Interface implements TestInterface
      {
         public static void main(String[] args)
         {
            TestInterface.method2();
         }
      }


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the program below:

      /**
      * Tested under Java 8 as well as OpenJDK 8
      */

      interface TestInterface
      {
         /*
          * why private access specifier is not allowed for method1 as we can call
          * method1()
          * from method2() irrespective of it's access specifier
          */

         // private static void method1()
         static void method1()
         {
            System.out.println("Inside method method1()");
         }

         static void method2()
         {
            System.out.println("Inside method method2(). Calling method method1()");
            method1();
         }
      }

      public class Java8Interface implements TestInterface
      {
         public static void main(String[] args)
         {
            TestInterface.method2();
         }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      private access specifier should be allowed for method method1()
      ACTUAL -
      private access specifier is not allowed for method method1()

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Illegal modifier for the interface method method1; only public, abstract, default, static and strictfp are permitted

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /**
      * Tested under Java 8 as well as OpenJDK 8
      */

      interface TestInterface
      {
         /*
          * why private access specifier is not allowed for method1 as we can call
          * method1()
          * from method2() irrespective of it's access specifier
          */

      // private static void method1()
         static void method1()
         {
            System.out.println("Inside method method1()");
         }

         static void method2()
         {
            System.out.println("Inside method method2(). Calling method method1()");
            method1();
         }
      }

      public class Java8Interface implements TestInterface
      {
         public static void main(String[] args)
         {
            TestInterface.method2();
         }
      }

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

            aefimov Aleksej Efimov
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: