Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-24-2007, 10:31 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
Rep Power: 0
ravian is on a distinguished road
Default Anonymous class
I am interested in using anonymous classes. I read somewhere that anonymous classes should be preferred when class have a very short body an only one instance of that class is needed.

Can some one guide me about the syntax and working of anonymous classes.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-24-2007, 11:42 AM
khamuruddeen's Avatar
Member
 
Join Date: Dec 2007
Posts: 24
Rep Power: 0
khamuruddeen is on a distinguished road
Default
I think its useful 2 u

Anonymous Classes in Java
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-24-2007, 10:00 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,389
Rep Power: 3
hardwired is on a distinguished road
Default
More (pseudo code) examples:
Code:
JButton button = new JButton("say something");
button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        System.out.println("something");
    }
});
SpinnerNumberModel model = new SpinnerNumberModel(10,0,100,1);
JSpinner spinner = new JSpinner(model);
spinner.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        int value = ((Integer)((JSpinner)e.getSource()).getValue()).intValue();
        tellSomeone(value);
    }
});
// Although not an anonymous listener in the strict
// sense, this approach can make sense sometimes:
JButton someButton = new JButton("compute");
final JTextField textField = new JTextField(12);
ActionListener al = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int n = Integer.parseInt(textField.getText());
        doSomething(n);
    }
};
someButton.addActionListener(al);
textField.addActionListener(al);
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-25-2007, 10:56 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
Rep Power: 0
ravian is on a distinguished road
Default
Thanks for the details.
I will try this out.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the Priority for execution of Interface class and a Abstract class Santoshbk Advanced Java 0 04-02-2008 07:04 AM
Accessing inner class from outer class (an example) Java Tip Java Tips 0 02-17-2008 09:03 AM
An example of accessing outer class from inner class Java Tip Java Tips 0 02-17-2008 09:01 AM
Inner class accessing outer class Java Tip Java Tips 0 02-17-2008 08:59 AM
Name of Anonymous class eva New To Java 1 12-31-2007 01:07 PM


All times are GMT +2. The time now is 12:30 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org