Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Static class declarations

Can a class -- inner or outer -- be declared static?

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

QCan a class (whether an inner or outer class) be declared static?

AIn order to understand the use of the static keyword in class declaration, we need to understand the class declaration itself. You can declare two kinds of classes: top-level classes and inner classes.

Top-level classes

You declare a top-level class at the top level as a member of a package. Each top-level class corresponds to its own java file sporting the same name as the class name.

A top-level class is by definition already top-level, so there is no point in declaring it static; it is an error to do so. The compiler will detect and report this error.

Inner classes

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

1. Anonymous. Anonymous classes are declared and instantiated within the same statement. They 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();
   }
});


Because an anonymous class doesn't have a normal class declaration where it's possible to use static, it cannot be declared static.

2. Local. Local classes are the same as local variables, in the sense that they're created and used inside a block. Once you declare a class within a block, it can be instantiated as many times as you wish within that block. Like local variables, local classes aren't allowed to be declared public, protected, private, or static.

Here's a code example:

//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. Member classes are defined within the body of a class. You can use member classes anywhere within the body of the containing class. You declare member classes when you want to use variables and methods of the containing class without explicit delegation.

The member class is the only class that you can declare static. When you declare a member class, you can instantiate that member class only within the context of an object of the outer class in which this member class is declared. If you want to remove this restriction, you declare the member class a static class.

When you declare a member class with a static modifier, it becomes a nested top-level class and can be used as a normal top-level class as explained above.

4. Nested top-level. A nested top-level class is a member classes with a static modifier. A nested top-level class is just like any other top-level class except that it is declared within another class or interface. Nested top-level classes are typically used as a convenient way to group related classes without creating a new package.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (9)
Login
Forgot your account info?

static methods -> static classBy Anonymous on June 29, 2009, 5:41 amwhat about declaring a static inner class only because all its methods are static!

Reply | Read entire comment

Simple and Great!By Anonymous on June 16, 2009, 1:37 amThere is no redundant description. Simple and easy to understand! ^^ Thanks.

Reply | Read entire comment

Top Levels classes and java source fileBy Anonymous on May 7, 2009, 9:57 pmJust a little correction, each Top-Level Class does NOT need to be in its own source file neither the file needs to have the same name as the class. This is required...

Reply | Read entire comment

MemberBy Anonymous on April 28, 2009, 2:53 amIt will be better if you provide example for 3rd and 4th type and second example is confusing

Reply | Read entire comment

Excellent articleBy Anonymous on March 5, 2009, 6:01 pmA straightforward explanation of a rather confusing and error-generating matter. Respect.

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources