Questions about Inner classes?
Hi,
I have a couple of questions about inner classes.
1. Can a named inner class be defined as non-private (e.g. public)? If yes, it means any other class can gain access to it. So why do we need a named inner class but not a public method instead?
2. When an anonymous class is created, are all attributes of the enclosing
class IMPLICITLY passed into the constructor (as arguments) of the object being created? For example:
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
}
});
Thank you very much!
SunnyDay
[629 byte] By [
Sunny_Daya] at [2007-10-2 15:28:31]

> 1. Can a named inner class be defined as non-private
> (e.g. public)? If yes, it means any other class can
> gain access to it. So why do we need a named inner
> class but not a public method instead?
It is possible to have a public inner class. There are two cases: a public static inner class functions like a regular class. In fact the standard java package contains some of these... I think things like RTFEditorKit.Bold or some such; a style. It is also possible to have a non-static public class. One case I could see for using this would be a TextComponent with a search feature. The text searching is explicitly part of the textcomponent but maintaining things like the location of the last search etc could be handled via an inner class
> 2. When an anonymous class is created, are all
> attributes of the enclosing
> class IMPLICITLY passed into the constructor (as
No. In the case of interfaces it acts exactly as if you'd implemented the interface with a 0-arg constructor. Otherwise you still need to use a constructor of the class your overriding
> Hi,
>
> I have a couple of questions about inner classes.
>
> 1. Can a named inner class be defined as non-private
> (e.g. public)? If yes, it means any other class can
> gain access to it. So why do we need a named inner
> class but not a public method instead?
>
I can't see how the two are equivalent. A named inner class is a fully fledged object class. For example Rectangle2D is abstract, and the concrete classes are inside (Rectangle2D.Doule, Rectangle2D.Float).
> 2. When an anonymous class is created, are all
> attributes of the enclosing
> class IMPLICITLY passed into the constructor (as
> arguments) of the object being created? For example:
It's not being an anonymous class that has the special access, it's a local class. You can also declare a local named class inside a method. An anonymous class gets it's access status from context, you can declare one as an initial value for a instance or static field, for example.
Local classes can access any local final variables that are declared before they are in the method. Actually the class secretly copies such variables (because the objects created could live longer than the stack frame in which the local variables exist).
This copying is part of their initialisation process, (only part of the instance intialisation of a class is running a constructor).