• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling method from anonymous inner class with return type results error but not with return type

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i tried to call method with return type"int" then it complied fine but when i tried to call with "void " it results " method declaration invalid..
Why so?
Here's the code



Moreover when line 15 is commented code compiles fine and outputs :
Pop
Pop.

Kindly any one please explain this to me .
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Divyya,

If i understand correctly you wanted to also run the method pop1: your problem is that you are trying to execute a method directly from the anonymous class, but when you write that method there the compiler seeks to override the previous one (mainly that's the scope of an anonymous class). So i propose the next solution (java code block) :

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Divyya Joshi wrote:



That's a strange construct you've got there. Can you confirm exactly what you're trying to do?

What you're actually doing is creating an anonymous inner class. The two lines 18/19 (in the original listing) are therefore interpreted as declarations within that class. So the first is fine - it declares x as a member variable. The second is not, as it isn't a declaration.

Alexandru's suggestion is one way of getting it to compile, but I'm not sure if it achieves what you're trying to achieve.
 
No more fooling around. Read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic