• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

anonymous inner class beginner basics

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 80617
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
sinatra roger
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80617
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic