I'm having trouble understanding inner and anonymous classes.
Can anyone show me a simple example of it's usage?
Would appreciate it!
Can anyone show me a simple example of it's usage?
Would appreciate it!
|
Java
Inner classes and anonymous classes |
|
A common inner class use is an actionlistener for a button. Most of the time you won't want to start another text file just for a simple listener on one button. Instead, at the bottom of your class you would make another class like:
Code private class MyListener implements ActionListener{This goes within the brackets of the other class but not in a method. If you have very complex listeners that handle alot of buttons you'd probably be better off making a whole new text file but for very simple things like this is when it is commonly used (Jun 11, 2012 03:50 PM)novados24 Wrote: A common inner class use is an actionlistener for a button. Most of the time you won't want to start another text file just for a simple listener on one button. Instead, at the bottom of your class you would make another class like: expanding on this, anonymous classes can be used to accomplish the same goal in a similar way: Code someButton.addActionListener(new ActionListener() {
Jun 12, 2012 12:43 AM
(This post was last modified: Jun 12, 2012 12:43 AM by irrationalByte.)
How about if you have have been given a project to make an Employee class?
Its easy,isn't it? Code public class EmployeeHint:Its not a compiling error but it voids a defintion. |
|
|
| Possibly Related Threads... | |||
| Thread / Author | Replies | Views | Last Post |
| [Java] by CheesyBreadJan 15, 2014 11:08 AM | 0 | 125 |
Jan 15, 2014 11:08 AM Last Post: CheesyBread |