Saturday, May 2, 2015

Defining an Inner Class inside an Interface

Any class that is defined within an Interface is set to be 'public static' by JVM, so the modifier 'static' is not necessary. A static member class is just like another class outside the OuterClass.

Defining a class inside an Interface is purely for the code readability purpose, there is no specially techniques here. For example, if a class is only used by one Interface, it helps the code readability by defining the class inside this Interface.

------------Java-------------------

public interface FunBoy {
int NUM = 3;
class Boy{}

}


-------------Bytecode---------------

public interface FunBoy
  SourceFile: "FunBoy.java"
  InnerClasses:
       public static #14= #12 of #1; //Boy=class FunBoy$Boy of class FunBoy
  minor version: 0
  major version: 50
  flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT
Constant pool:
   #1 = Class              #2             //  FunBoy
   #2 = Utf8               FunBoy
   #3 = Class              #4             //  java/lang/Object
   #4 = Utf8               java/lang/Object
   #5 = Utf8               NUM
   #6 = Utf8               I
   #7 = Utf8               ConstantValue
   #8 = Integer            3
   #9 = Utf8               SourceFile
  #10 = Utf8               FunBoy.java
  #11 = Utf8               InnerClasses
  #12 = Class              #13            //  FunBoy$Boy
  #13 = Utf8               FunBoy$Boy
  #14 = Utf8               Boy
{
  public static final int NUM;
    flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL
    ConstantValue: int 3

}

No comments:

Post a Comment