Java Reference
In-Depth Information
public class AnOuterClass extends JApplet
{
int fVar = 0;
JButton fBut = new JButton ("OK");
public AnOuterClass () {
class AnInnerClass implements ActionListener
{
public void actionPerformed (ActionEvent e) {
fVar++;
System.out.println ( " Pressed " + fVar + " times " );
}
} // class AnInnerClass
fBut.addActionListener (new AnInnerClass ());
add (fBut);
} // ctor
} // class AnOuterClass
The compiler will separate out these inner classes and create separate class
files for them. The names will be preceded with the outer class name, as in
AnOuterClass$AnInnerClass.class
for the above example.
7.5.2 Anonymous inner classes
It is common in Java programming to find situations where you need to create an
object but don't need to bother with giving it an explicit name. For example, to
specify the size of a panel you could use code like this:
Dimension d = getSize ();
int width = d.width;
int height = d.height;
where a Dimension object is returned from a method and then used to specify
the width and height values. But why bother to create a variable name for this
Dimension object since it will never be used again? Instead, you could replace
the code with
int width = getSize ().width;
int height = getSize ().height;
 
Search WWH ::




Custom Search