Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Instantiation of a Listener using an anonymous inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Instantiation of a Listener using an anonymous inner class" Watch "Instantiation of a Listener using an anonymous inner class" New topic
Author

Instantiation of a Listener using an anonymous inner class

Mark Do
Ranch Hand

Joined: Mar 18, 2014
Posts: 70


Can a method in an anonymous inner class ever be private? why or why not?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Why don't you try out if you can add a private method to the anonymous inner class?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mark Do
Ranch Hand

Joined: Mar 18, 2014
Posts: 70
I've tried changing it to a private scope, and an error comes up.

I don't understand why it can't be private..
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

A child class needs to satisfy the super class contract. When you narrow down the scope, you break the conract


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

The actionPerformed method cannot be made private, because it's a method that's inherited from the interface that the anonymous class is inheriting. But this, by itself, has nothing to do with the fact that it's an anonymous inner class. Methods declared in interfaces are always public, so in a class that implements an interface (no metter if it's an anonymous inner class or a regular class), the methods inherited from the interface must always be public.

But you can add private methods if you like, for example:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
Use a different listener for each colour. That block of ifs is in my opinion not object‑oriented. Search my posts in the Swing forum for "ColourListener".

As for Swing forum, I think this discussion would sit better there, so I shall move it.
Mark Do
Ranch Hand

Joined: Mar 18, 2014
Posts: 70
Thanks guys! that can't be any clearer!
Mark Do
Ranch Hand

Joined: Mar 18, 2014
Posts: 70
Is it right to say:
• The parameter ‘e’ can be renamed as long as its type is the same.
• The class that has the interface type InnerMenuListener (deduced from the constructor) is the anonymous class definition in the code.
• actionPerformed CANNOT be renamed because it is implementing an interface, therefore the names are prescribed by the interface.

I'm not too sure how to answer this question though?
What is referenced by ‘e’, the parameter of the actionPerformed(…) method?


Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instantiation of a Listener using an anonymous inner class