• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Anonymous classes

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
regarding anonymous classes
Is this statement true ?
An anonymous class can only be created within the body of a method
If false where else can an anonymous class be created. please explain
Amit
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anonymous classes are used in the context of a method...as
1) return value of a method
2) argument in a method call
3) in initialization of variables.
Waiting for some examples for each usage...in your answer!!!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// creates an anonymous class which implements the interface ActionListener, and passes as an argument to the addActionListener() method
myButton.addActionListener(new ActionListener() {
.......
});
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Anonymous Inner classes can be written not only inside methods
but also in static initializers and instance initializers.
class a {
static {
Button b1 = new Button("kishan");
b1.addActionListener(new ActionListener() {.....});
}
{
Button b1 = new Button("Amit");
b1.addActionListener(new ActionListener() {.....});
}
}
Remember the words "this" and "super" cannot be used in static
initializers whereas it can be used in Instance initializers.
Hope that helps

------------------
Regards,
V. Kishan Kumar
 
faiza haris
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain....
Anonymous inner class can be defined in contexts where a reference can be used.?
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] arr = {1, 2, 3, 4, 5};
somefunc(arr); // call somefunc passing array
System.out.println(arr[1]); // prints second integer
'arr' is a reference to an array of 5 integers. Any place you can use 'arr' in your code, you could use an anonymous class (remember an array IS an object.)
somefunc(new int[] {1, 2, 3, 4, 5}); // same thing
// can't get to array, no reference!
The big difference is I don't have a reference to the anonymous array to use anywhere else, other than to where it was passed (somefunc()).

 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic