Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Inner Class

 
Ivy Kho
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A) Will this example run ?

public class MyClass{
public void aMethod(Event e){
new ActionLister(Event e){
// some line here
}
}

}

B) I'm reading several notes about inner classes (aside from the Java Certification book by Heller and Roberts) and now, I'm pretty much confused.

From the book, I thought there were 2 types of inner classes -- member inner class or inner class (those declared inside a method / block). The latter (which is inner class) can be anonymous or not.

So, when I read this guy's notes (see #21 in http://www.geocities.com/SiliconValley/Network/3693/obj_sec6.html#obj19 ), he says there are 4 types -- Nested top-level classes, Member classes, Local classes, Anonymous classes.

1. What's the difference between the nested top-level classes from the member classes?
2. Can member inner classes be anonymous?

I apologize if I keep posting my questions. I'm studying by chapter and I do not want to move on without fully understanding this one.

Pls help. Thank you in advance.
[ September 01, 2005: Message edited by: Ivy Kho ]
 
Kalyana Sundaram
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

will this example run ?

public class MyClass{
public void aMethod(Event e){
new ActionLister(){
public void actionPerformed(ActionEvent e) {
// do something
}

// some line here
};
}

}

Check the following to make the class compile without errors
1) The intention is to create an annonymous class which implements an interface called ActionListener. Hence the Event e should be removed

2) a semicolon is missing at the end of the annonymous class definttion

3) The method actionPerformed() declared in the interface has to be implemented

4) check for typo errors.

Hey rest of the Guys out there, let me know if I miss out anything
[ September 01, 2005: Message edited by: Kalyana Sundaram ]
 
Abhishek Jain
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivy Kho

U should refer to kathy seirra material for Anonynmous classes it would help u to prevent making such mistakes.................

Regards
Abhi
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A*B*H*I", please read our JavaRanch Naming Policy, and change your displayed name to comply. We need a displayed name in the format <first name><space><family name>, preferably your real name.
Your membership of this site will be suspended if you maintain your current displayed name.

(NR)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic