wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner Class" Watch "Anonymous Inner Class" New topic
Author

Anonymous Inner Class

Ivy Kho
Greenhorn

Joined: Oct 20, 2004
Posts: 17
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

Joined: Mar 18, 2005
Posts: 94

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 ]

Only those who will risk going too far can possibly find out how far one can go !!!
Abhishek Jain
Greenhorn

Joined: Sep 02, 2005
Posts: 26
Ivy Kho

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

Regards
Abhi


SCJP <br />SCWCD <br />SCBCD <br />SCEA Preparing
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"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)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner Class
 
Similar Threads
Jyohti's URL for web page development needed
I need Jyoti Notes Link
When is an anonymous inner class NOT an inner class? Always?
Inner classes
Need Jyoti's notes' link