J2EEOnline J2EEOnline

Classes/Objects  «Prev 

Four Types of Static Nested Classes

You can define a class (or interface) as a static member inside another class (or interface). Since the outer type can be a class or an interface and the inner ones can also be a class or interface, there are four combinations.
The following are examples of the four types of static nested classes so that you can see their syntax:

class Outer{ // 1) an outer class has a static nested class
 static class Inner{}

interface Outer{ // 2) an outer interface has a static nested class
 static class Inner{}

class Outer{ // 3) an outer class has a static nested interface
 static interface Inner{}

interface Outer{ // 4) an outer interface has a static nested interface
 static interface Inner{}

You do not have to explicitly use the static keyword with a nested interface, since it is implicitly static.

Here are several characteristics of static nested classes (and interfaces).
  1. The accessibility (public, protected) of the static nested class is defined by the outer class.
  2. The name of the static nested class is expressed with
  3. When you define an inner nested class (or interface) inside an interface, the nested class is declared implicitly public and static. This point is easy to remember: any field in an interface is implicitly declared public and static, and static nested classes have this same behavior.
  4. Static nested classes can be declared abstract or final.
  5. Static nested classes can extend another class or it can be used as a base class.
  6. Static nested classes can have static members .
  7. Static nested classes can access the members of the outer class.
  8. The outer class can also access the members (even private members) of the nested class through an object of the nested class.
    If you do not declare an instance of the nested class, the outer class cannot access the nested class elements directly.

package com.java.classdesign;

import java.util.Arrays;
import java.util.Comparator;
public class SortLanguage {
 public static void main(String[] args) {
   String [] sa = {"JDBC", "Perl", "ASP","EJB", "JavaBeans" };
   Sorter s = new Sorter();
     for(String s2:sa){
       System.out.print(s2 + ", ");
   for(String s2:sa){
     System.out.print(s2 + ", ");
 /* Static Nested Class: Sorter has the non-access 
modifier 'static' and is declared within Pockets */
 static class Sorter implements Comparator< String >{
   public int compare(String a, String b) {
     //return b.compareTo(a); //descending order
     return a.compareTo(b); // //ascending order
JDBC, Perl, ASP, EJB, JavaBeans, 
ASP, EJB, JDBC, JavaBeans, Perl, 

Threads used with a static nested class

What will the following code print when compiled and run?
package threads;
public class RunTest {
 static class Runner implements Runnable{
  public void run(){
   System.out.println("In Run");
 public static void main(String[] args) {
  Runner r = new Runner();
  Thread t1 = new Thread(r);
  Thread t2 = new Thread(r);

Select 1 option:
  1. In Run
    In Run
  2. In Run
  3. It will not print anything.
  4. It will throw an exception at run time.

Answer: a
This code has two different Thread instances (thus two distinct threads) and both are started. The fact that the same Runnable instance is used by both threads is immaterial.
Both the threads will run independently and thus the run method of the Runnable will be invoked twice.