(x x, 1999)

The JavaWorld experts answer your most pressing Java questions -- every week

Q
Can a class (whether an inner or outer class) be declared static? How does it behave when static?

A

A top-level class is declared at the top level as a member of a package. Each top-level class corresponds to it's own java file sporting the same name as the class name.

An inner class is defined within a top-level class. Depending on how it is defined, an inner class can be one of the following four types:

  1. Anonymous classes are declared and instantiated within the same statement. Anonymous classes do not have names, and they can be instantiated only once. The following is an example of an anonymous class:

    okButton.addActionListener( new ActionListener(){
       public void actionPerformed(ActionEvent e){
          dispose();
       }
    });
    
  2. Local classes are defined within a code block. Once you declare a class with in a block, it can be instantiated as many times as you want within that block. Here is a code example:

    file://some code block .......{
       class ListListener implements ItemListener {
          List list;
          public ListListener(List l) {
             list = l;
          }
          public void itemStateChanged(ItemEvent e) {
             String s = l.getItemSelected();
             doSomething(s);
          }
       }
       List list1 = new List();
       list list2 = new List();
       list1.addItemListener(new ListListener(list1));
       list2.addItemListener(new ListListener(list2));
    }
    
  3. Member classes are defined within the body of a class. Member classes can be used anywhere within that class. You declare member classes when you want to use variables and methods of the containing class without explicit delegation.

  4. Nested top-level classes are same as member classes except that they are declared as static. A nested top-level class is just like any other top-level class except that it is declared within another class or interface. One reason to declare nested top-level classes is to reduce the name space pollution.

    If your main class has a few smaller helper classes that can be used outside the class and make sense only with your main class, it is a good idea to make them nested top-level classes. When you want to use the nested top-level class you would say: TopLevelClass.NestedClass. See the following example:

    public class Filter {
       Vector criteria = new Vector();
       public addCriterion(Criterion c) {
          criteria.addElement(c);
       }
       public boolean isTrue(Record rec) {
          for(Enumeration e=criteria.elements();
          e.hasMoreElements();) {
             if(! ((Criterion)e.nextElement()).isTrue(rec))
                 return false;
          }
          return true;
       }
       public static class Criterion {
          String colName, colValue;
          public Criterion(Stirng name, String val) {
             colName = name; colValue = val;
          }
          public boolean isTrue(Record rec) {
             String data = rec.getData(colName);
             if(data.equals(colValue)) return true;
             return false;
          }
       }
    }
    

    And when you want to use it:

    Filter f = new Filter();
    f.addCriterion(new Filter.Criterion("SYMBOL", "SUNW"));
    f.addCriterion(new Filter.Criterion("SIDE", "BUY"));
    .....
    if(f.isTrue(someRec)) file://do some thing .....
    

    Note that if you declare a member class as static it becomes a top-level class. However, it is an error to declare a top-level class as static because it is already a top-level class. You cannot, therefore, declare a top-level class as static.

Related: