• 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

method local class problem

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can i return an object of the method local inner class created inside the same method. If yes, then how am I going to write the return type of the method which contains the class?

Thanks in advance.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh..!! Well Rohit..I do not think that we can do it..!! Feeling a little lazy to write code and check that..!! But I can try to give some excuse to validate point..!!

1) first of all at the time of method Declaration we don't know about the method~local~inner~class..we don't know even the name of the class..!! we don't know whether it exist or not..!! so how can we declare the return type of the same..!!

2) method~local~inner~class is local to method..even if we pass it outside..we wont be able to access the method define inside it..!! So there is no benefit..!!

3) What if we declare a method in an interface or abstract method..!! this is just like at the time of declaration we are forcing the implementing class to define a class..!! we can not do that..we can not say anything about method content at the time of declaration..all we can say what parameter it can accept..what it can return..!!

I am not sure about any point..!! This is my thinking process..!! I am out of excuses now.!!!
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Sunz,

Have a look at this... It compiles!!

 
Rohit Garg
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Thanks for the post Vishwanath as it got one more of my doubts cleared out.

What i wanted to exactly ask is

Can i replace <something> with a valid class name so that the code compiles
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the only type that <something> could be is 'Object' or if the Local class extends or implements some known class or interface(i.e known outside the method) then you could easily return that type or cast to it later within main().
 
Rohit Garg
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for clearing my doubt.
 
My, my, aren't you a big fella. Here, have a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic