inner classes and other stuff...  
Author Message
Frances





PostPosted: 2005-9-6 9:36:00 Top

java-programmer, inner classes and other stuff... from latest oreilly Learning Java, pg 623:
www.francesdelrio.com/java/TextEntryBox.java (SSCCE)

I have a few questions about this example:

1. a second class gets created (TextEntryBox$1.class) when this is
compiled, even though there's no inner class here..

2. it seems like a new instantation of ActionListener is being passed
in a meth arg.....
( field.addActionListener(new ActionListener( ) { )

when in fact ActionListener is an interface and as such cannot be
instantiated, only implemented.. (as far as I know...;)

3. and this ActionListener interface is not implemented anywhere here
that I can see....;)

could someone pls explain these things that seem contrary to what I have
read... (i.e., what I THOUGHT I understood... oh brother....)
thank you...

(PS: also, pls, why do some methods have a ');' after end of method
declaration?)


 
Patricia Shanahan





PostPosted: 2005-9-6 9:46:00 Top

java-programmer >> inner classes and other stuff... Frances wrote:
> from latest oreilly Learning Java, pg 623:
> www.francesdelrio.com/java/TextEntryBox.java (SSCCE)
>
> I have a few questions about this example:
>
> 1. a second class gets created (TextEntryBox$1.class) when this is
> compiled, even though there's no inner class here..
>
> 2. it seems like a new instantation of ActionListener is being passed
> in a meth arg.....
> ( field.addActionListener(new ActionListener( ) { )
>
> when in fact ActionListener is an interface and as such cannot be
> instantiated, only implemented.. (as far as I know...;)
>
> 3. and this ActionListener interface is not implemented anywhere here
> that I can see....;)
>
> could someone pls explain these things that seem contrary to what I have
> read... (i.e., what I THOUGHT I understood... oh brother....)
> thank you...
>
> (PS: also, pls, why do some methods have a ');' after end of method
> declaration?)
>
>

You are missing one concept that underlies all those issues, anonymous
inner classes.

An anonymous inner class is an inner class that is declared, and a
single instance of the class created, without the inner class being
given a name.

The whole of:

new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
area.append(field.getText( ) + '\n');
field.setText("");
}
}

is an expression whose value is a reference to a new instance of an
anonymous class implementing ActionListener, with:

public void actionPerformed(ActionEvent ae) {
area.append(field.getText( ) + '\n');
field.setText("");
}

as the only declaration in the body of the class.

Patricia




 
Roedy Green





PostPosted: 2005-9-6 11:15:00 Top

java-programmer >> inner classes and other stuff... On Mon, 05 Sep 2005 21:36:06 -0400, Frances <email***@***.com> wrote or
quoted :

>1. a second class gets created (TextEntryBox$1.class) when this is
>compiled, even though there's no inner class here..

there is an anonymous class:

field.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ae) {
area.append(field.getText( ) + '\n');
field.setText("");
}

See http://mindprod.com/jgloss/nestedclasses.html
and follow links.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
Roedy Green





PostPosted: 2005-9-6 11:16:00 Top

java-programmer >> inner classes and other stuff... On Mon, 05 Sep 2005 21:36:06 -0400, Frances <email***@***.com> wrote or
quoted :

> when in fact ActionListener is an interface and as such cannot be
>instantiated, only implemented.. (as far as I know...;)

which is exactly what is happening. You are writing a miniature inline
anonymous class that implements the ActionListener Interface.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
Roedy Green





PostPosted: 2005-9-6 11:17:00 Top

java-programmer >> inner classes and other stuff... On Mon, 05 Sep 2005 21:36:06 -0400, Frances <email***@***.com> wrote or
quoted :

>could someone pls explain these things that seem contrary to what I have
>read... (i.e., what I THOUGHT I understood... oh brother....)
> thank you...

Check out "anonymous class" in your text book.

See http://mindprod.com/jgloss/anonymousclasses.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
Frances





PostPosted: 2005-9-8 13:16:00 Top

java-programmer >> inner classes and other stuff... Patricia Shanahan wrote:
> Frances wrote:
>
>> from latest oreilly Learning Java, pg 623:
>> www.francesdelrio.com/java/TextEntryBox.java (SSCCE)
>>
>> I have a few questions about this example:
>>
>> 1. a second class gets created (TextEntryBox$1.class) when this is
>> compiled, even though there's no inner class here..
>>
>> 2. it seems like a new instantation of ActionListener is being passed
>> in a meth arg.....
>> ( field.addActionListener(new ActionListener( ) { )
>> when in fact ActionListener is an interface and as such
>> cannot be instantiated, only implemented.. (as far as I know...;)
>>
>> 3. and this ActionListener interface is not implemented anywhere here
>> that I can see....;)
>>
>> could someone pls explain these things that seem contrary to what I
>> have read... (i.e., what I THOUGHT I understood... oh brother....)
>> thank you...
>>
>> (PS: also, pls, why do some methods have a ');' after end of method
>> declaration?)
>>
>>
>
> You are missing one concept that underlies all those issues, anonymous
> inner classes.
>
> An anonymous inner class is an inner class that is declared, and a
> single instance of the class created, without the inner class being
> given a name.
>
> The whole of:
>
> new ActionListener( ) {
> public void actionPerformed(ActionEvent ae) {
> area.append(field.getText( ) + '\n');
> field.setText("");
> }
> }
>
> is an expression whose value is a reference to a new instance of an
> anonymous class implementing ActionListener, with:
>
> public void actionPerformed(ActionEvent ae) {
> area.append(field.getText( ) + '\n');
> field.setText("");
> }
>
> as the only declaration in the body of the class.
>
> Patricia

Thank you very much Patricia.. and thank you also for response to my
other post (related to this one);.....;)

so what is passed in argument to field.addActionListener() method is
class we're creating on the fly.. (a sort of ad-hoc class that ceases to
exist afterwards... and that in this case that implements
ActionListener..) and the ');' closes parens that contains entire
argument being passed to method... (which is body of class and
constructor-call to create an obj of it.... ok, I hope I got it about
right this time...:)
again, thank you very much...
Frances



 
 
Monique Y. Mudama





PostPosted: 2005-9-9 3:00:00 Top

java-programmer >> inner classes and other stuff... On 2005-09-08, Frances penned:
> Patricia Shanahan wrote:
>>
>> An anonymous inner class is an inner class that is declared, and a
>> single instance of the class created, without the inner class being
>> given a name.
>>
>> The whole of:
>>
>> new ActionListener( ) {
>> public void actionPerformed(ActionEvent ae) {
>> area.append(field.getText( ) + '\n');
>> field.setText("");
>> }
>> }
>>
>> is an expression whose value is a reference to a new instance of an
>> anonymous class implementing ActionListener, with:
>>
>> public void actionPerformed(ActionEvent ae) {
>> area.append(field.getText( ) + '\n');
>> field.setText("");
>> }
>>
>> as the only declaration in the body of the class.
>>
>> Patricia
>
> Thank you very much Patricia.. and thank you also for response to my
> other post (related to this one);.....;)
>
> so what is passed in argument to field.addActionListener() method is
> class we're creating on the fly.. (a sort of ad-hoc class that ceases to
> exist afterwards... and that in this case that implements
> ActionListener..) and the ');' closes parens that contains entire
> argument being passed to method... (which is body of class and
> constructor-call to create an obj of it.... ok, I hope I got it about
> right this time...:)
> again, thank you very much...
> Frances

Well, no, it doesn't cease to exist. It continues to exist and do its
thing.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html