JavaNestedClasses: Difference between revisions

From Wiki RB4
(New page: ==Inner Classes== Inner classes are a new feature of Java 1.1. An inner class is a class that is defined inside another class. Inner classes can access everything of the outer class. Inne...)
 
Line 1: Line 1:
==Inner Classes==
==Inner Classes==
Inner classes are a new feature of Java 1.1. An inner class is a class that is defined inside another class. Inner classes can access everything of the outer class. Inner classes are the only classes that can be private. Es gibt vier Arten von inner classes:
An inner class in contrast to a top level class is a class that is defined inside another class. Inner classes can access everything of the outer class. Inner classes are the only classes that can be private. Es gibt vier Arten von inner classes:
*statische innere Klassen (nested top-level class)
*statische innere Klassen (nested top-level class)
*Elementklasse (member class)
*Elementklasse (member class)
*Lokale Klassen (in Methoden)
*Lokale Klassen (in Methoden)
*Anonyme innere Klassen
*Anonyme innere Klassen
===static inner classes===
Static inner classes or nested top level classes are declared and used like this:
public class Lamp
{
  static String s = "hi";
  int i = 1;
  static class Bulp
  {
    void printHi()
    {
      System.out.println( s );
      // System.out.println( i );          // error, because i is not static
    }
  }
  public static void main(String[] args)
  {
    Bulp bulp = new Lamp.Bulp(); 
    bulp.printHi();
  }
}
There is no difference between a top level class and a nested top level class. You don't need an object of the outer class to use it.


Inner classes can be defined in different locations: instead of an operation or locally inside an operation. In the second case you even don’t have to specify a name for the class.  
Inner classes can be defined in different locations: instead of an operation or locally inside an operation. In the second case you even don’t have to specify a name for the class.  

Revision as of 23:11, 3 January 2008

Inner Classes

An inner class in contrast to a top level class is a class that is defined inside another class. Inner classes can access everything of the outer class. Inner classes are the only classes that can be private. Es gibt vier Arten von inner classes:

  • statische innere Klassen (nested top-level class)
  • Elementklasse (member class)
  • Lokale Klassen (in Methoden)
  • Anonyme innere Klassen

static inner classes

Static inner classes or nested top level classes are declared and used like this:

public class Lamp
{ 
  static String s = "hi"; 
  int i = 1; 

  static class Bulp
  { 
    void printHi() 
    { 
      System.out.println( s ); 
      // System.out.println( i );          // error, because i is not static 
    } 
  } 

  public static void main(String[] args) 
  { 
    Bulp bulp = new Lamp.Bulp();  
    bulp.printHi(); 
  } 
}

There is no difference between a top level class and a nested top level class. You don't need an object of the outer class to use it.


Inner classes can be defined in different locations: instead of an operation or locally inside an operation. In the second case you even don’t have to specify a name for the class.

Der Zugriff auf die äußere Klasse erfolgt über den Klassennamen:

  x = Haus.this.<Member>;

Innere Klassen können wie folgt erzeugt werden:

  Haus h = new Haus();
  Zimmer z = h.new Zimmer();
  // oder
  z = new Haus().new Zimmer();

Inner classes are handled by the compiler. The virtual machine does not know anything about it. The name of the inner class is <OuterClassName>$<InnerClassName> and there are normal class files generated prefixed with an $.


Anonymous classes

An anonymous class behaves just like a local class, and is distinguished from a local class merely in the syntax used to define and instantiate it. Anonymous classes can not have an ctor because the ctor must have the same name as the class and this class doesn’t have a name. There is another feature, accessing parameters, which is very special and not recommended.

Die Declaration erfolgt durch:

new <Classname>(<optional argument list>)
{
  <ClassBody>
}

An example of such an anonymous class is:

AddMouseListener(new MouseAdapter() 
                     { 
                       public mousePressed() 
                       {
                         ...
                       }   
                     } 
                );