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
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2016-05-03
  • Updated: 2017-05-04
  • Resolved: 2017-05-04
Related Reports
Duplicate :  
Description
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 ----------


Comments
When an interface that contains a static method is implemented, the static method is still part of the interface and not part of the implementing class. So, its secure from any kind of modification by other classes. And the purpose of static methods in an interface is to provide utility methods in the interface itself, rather than using a companion utility class, hence ,the implicit public modifier .
19-05-2016