devxlogo

Inner Classes

Inner Classes

Question:
Why can’t I have a static member in an inner class?

Answer:
The simple answer is that static member variables and methods aredisallowed by the Inner Classes Specification that was introduced withJava 1.1. But that alone will not satisfy most people looking for ananswer to this question. You want to know why this is disallowed bythe specification. Before moving on to that, I should distinguish between an inner class and a static inner class. An inner class is a class declared within the scope of another class, either local to aclass declaration (i.e., as a class member), a block of statements, oran expression (in which case it is an anonymous class). However, it ispossible to define a static inner class within a top-level classdeclaration. A class defined in this way is itself treated as atop-level class, meaning that it can only access its own members andstatic members of its enclosing class. But since it is declaredwithin the scope of another class, it cannot be referencedoutside of the enclosing class without specifying a qualified name(e.g., OuterClassName.InnerClassName).

Inner classes cannot have static members because static members aretop-level lexical entities. In other words, static members areaccessible outside of a given class’s scope. Inner classes themselvesare not top-level entities, being bound to specific instances of theirenclosing class. If you feel you really must have an inner class to have staticmember, just define that variable as a member of the enclosing class.But it so happens that static inner class can have static members.This is because they are top-level classes, and as such are not boundto specific instances of their enclosing class.The best way to think about all of this is to consider inner classesand static inner classes as two different animals by introducing theterm nested class, which is loosely used in the Inner ClassesSpecification. Think of both types of classes as nested classes, andonly use the term inner class to refer to non-static nested classes.Therefore a static nested class is not really an inner class, and canhave static members. The following example differentiates thebehavior of inner classes and static nested classes with respect tostatic members:

See also  Why ChatGPT Is So Important Today

public class InnerClassExample {  private String __privateMember = "__privateMember";  private static String __staticPrivateMember = "__staticPrivateMember";  // This is an inner class, it cannot have static members.  public class InnerClass1 {    public void print() {      System.out.println(__privateMember);      System.out.println(InnerClass2.__staticInnerClass2Member);    }  }  // This is a static nested class, which makes it a top-level class,  // allowing it to have static members.  public static class InnerClass2 {    public static String __staticInnerClass2Member =      "__staticInnerClass2Member";    public void print() {      // Accessing __privateMember would be a compile-time error.      // InnerClass2 is a top-level class and can only reference its      // own member variables and the static variables of its lexically      // encompassing class, InnerClassExample.      System.out.println(__staticPrivateMember);      System.out.println(__staticInnerClass2Member);    }  }  public void testInnerClasses() {    InnerClass1 class1 = new InnerClass1();    InnerClass2 class2 = new InnerClass2();    class1.print();    class2.print();  }  public static final void main(String[] args) {    InnerClassExample example = new InnerClassExample();    example.testInnerClasses();  }}
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist