DEV Community

Cover image for Inner Classes
Paul Ngugi
Paul Ngugi

Posted on

Inner Classes

An inner class, or nested class, is a class defined within the scope of another class. Inner classes are useful for defining handler classes.

Inner classes are used in the preceding post. This section introduces inner classes in detail. First, let us see the code in Figure below. The code in Figure below (a) defines two separate classes, Test and A. The code in Figure below (b) defines A as an inner class in Test.

Image description

The class InnerClass defined inside OuterClass in Figure above (c) is another example of an inner class. An inner class may be used just like a regular class. Normally, you define a class as an inner class if it is used only by its outer class. An inner class has the following features:

  • An inner class is compiled into a class named OuterClassName$InnerClassName.class. For example, the inner class A in Test is compiled into Test$A.class in Figure above (b).
  • An inner class can reference the data and the methods defined in the outer class in which it nests, so you need not pass the reference of an object of the outer class to the constructor of the inner class. For this reason, inner classes can make programs simple and concise. For example, circlePane is defined in ControlCircle.java in here (line 16). It can be referenced in the inner class EnlargeHandler in line 53.
  • An inner class can be defined with a visibility modifier subject to the same visibility rules applied to a member of the class.
  • An inner class can be defined as static. A static inner class can be accessed using the outer class name. A static inner class cannot access nonstatic members of the outer class.
  • Objects of an inner class are often created in the outer class. But you can also create an object of an inner class from another class. If the inner class is nonstatic, you must first create an instance of the outer class, then use the following syntax to create an object for the inner class:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

  • If the inner class is static, use the following syntax to create an object for it:

OuterClass.InnerClass innerObject = new OuterClass.InnerClass();

A simple use of inner classes is to combine dependent classes into a primary class. This reduces the number of source files. It also makes class files easy to organize since they are all named with the primary class as the prefix. For example, rather than creating the two source files Test.java and A.java as shown in Figure above (a), you can merge class A into class Test and create just one source file, Test.java as shown in Figure below (b). The resulting class files are Test.class and Test$A.class.

Another practical use of inner classes is to avoid class-naming conflicts. Two versions of CirclePane are defined in here ControlCircleWithoutEventHandling.java and ControlCircle.java. You can define them as inner classes to avoid a conflict.

A handler class is designed specifically to create a handler object for a GUI component (e.g., a button). The handler class will not be shared by other applications and therefore is appropriate to be defined inside the main class as an inner class.

Top comments (0)