Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

anonymous inner class beginner basics  RSS feed

 
thejwal pavithran
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i am learning about anonymous inner classes and wrote some code to help me understand it :


I tried google to clear my doubts but still i wanted to get some expert help.

So what really is the use of anonymous inner classes? is it used when there is no need to create a separate class(to reduce typing?)?.

Is there any way an instance of an anonymous inner class can be created in the main function or in some other class?(an instance of subclass of ferrari in the above code?)

Please tell me if my thinking is correct and please add any important points regarding anonymous inner classes.

thanks
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejwal pavithran wrote:hi guys,

i am learning about anonymous inner classes and wrote some code to help me understand it :
I tried google to clear my doubts but still i wanted to get some expert help.

So what really is the use of anonymous inner classes? is it used when there is no need to create a separate class(to reduce typing?)?.

Is there any way an instance of an anonymous inner class can be created in the main function or in some other class?(an instance of subclass of ferrari in the above code?)

Please tell me if my thinking is correct and please add any important points regarding anonymous inner classes.

thanks

An anonymous inner class is just a Method Local implementation of an existing class or interface...
For a better understanding you should go through this javaranch page..
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don’t have an anonymous local class. Count the {} carefully. That anonymous class is outside any methods, and the way it is written, is never used.
 
thejwal pavithran
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont really get your point. the code compiles and executes.

i modified the code in the following page:
http://inheritingjava.blogspot.in/2011/02/chapter-54-anonymous-inner-classes.html
anyway im still studying and hope to get it soon
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejwal pavithran wrote:i dont really get your point. the code compiles and executes.

i modified the code in the following page:
http://inheritingjava.blogspot.in/2011/02/chapter-54-anonymous-inner-classes.html
anyway im still studying and hope to get it soon

Well, your code is syntactically correct..
What Cambell is saying is, anonymous class is mostly used inside a method.. That's why I wrote there - "Method Local definition"...
But here you have used it ouside any method of that class..
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code might be correct syntax, but it is poor style. You have a field of the class which is not marked private and it is not used inside the class.
There are far better examples of anonymous inner classes going, for example the Runnable in the second code example in the javax.swing package documentation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!