led said:
hello..
I'm a beginner in Java, so I'm learning from "Thinking in Java" by BE
I've came to the chapter interfaces & inner classes
so my quest. is: how often do you use inner classes?
they simply don't make sesnse to me...or am I wrong? and
should I study them much deeper?
They are definately worth using, especially as you move into doing programs
of realistic size and complexity. Anonymous inner classes are widely used
for GUI event handlers because they allow you to specify functionality that
is separate from the GUI class but that will execute in the context of that
class.
myButton.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent actionEvent) {
// Write your code here that will be executed when the button is pressed
// It can use the instance variables of the enclosing class.
}
});
Notice that the ActionListener is actually an Interface. For some listeners
that have many methods there are default implementations that have empty
methods that you can use so that you only have to provide an implementation
for the one you want, as in using a MouseAdapter or WindowAdapter (check
these out in the API)
I often use named inner classes for specialized GUI data structures, such as
special kinds of JTree nodes or Cell Renderers and so forth.
Matt Humphrey (e-mail address removed)
http://www.iviz.com/