Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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()).

 
Won't you be my neighbor? - Fred Rogers. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic