Java - static Members in an Inner Class

Introduction

The keyword static in Java is for a top-level construct.

You cannot declare any static members (fields, methods, or initializers) for an inner class.

The following code will not compile because inner class B declares a static field DAYS_IN_A_WEEK:

class A {
  public class B {
    // Cannot have the following declaration
    public static int V = 7; // A compile-time error
  }
}

It is allowed to have static fields in an inner class that are compile-time constants.

Demo

class A {
  public class B {
    // Can have a compile-time static constant field
    public final static int V = 7; // OK

    public final String str = new String("Hello");
  }// w  w w .j  a va2  s  .co  m
}

public class Main{
  public static void main(String[] argv) {
    System.out.println(A.B.V);
    A a = new A();
    A.B b = a.new B();
    System.out.println(b.str);
  }
}

Result