This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes anonymous inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "anonymous inner classes" Watch "anonymous inner classes" New topic
Author

anonymous inner classes

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
>>>Inner classes can be created within methods. This is something that GUI builders like Borland JBuilder do a great deal of when creating Event handlers.

Here is an example of such automatically generated code

buttonControl1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
buttonControl1_mouseClicked(e);
}
});



>>>


As you might guess an anonymous class cannot have be given a constructor by the programmer. Think about it, a constructor is a method with no return value and the same name as the class. Duh! we are talking about classes without names. An anonymous class may extend another class or implement a single interface

>>>
A class defined within a method can only access fields in the enclosing method if they have been defined as final. This is because variables defined within methods normally are considered automatic, ie they only exist whilst the method is executing. Fields defined within a class created within a method may outlive the enclosing method.




I was reading above lines from link
http://www.jchq.net/certkey/0401certkey.htm

did not understand it clearly.


Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
There are several errors in your quotes. A constructor isn't a method. The behaviour of local variables is described correctly, but we do not usually use the word automatic to describe such behaviour. The code you quote contains a compiler error.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: anonymous inner classes