• 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

Could we have classes inside methods

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai ,
could we have classes inside methods, so
whenever we call a method so that the other
class also gets instantiated and can we use the
other class method from the method we call.
thanks
regards
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have classes inside a method but that class would only exist until the program exited the method.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, those would be local classes.
From the JLS: 14.2 Local Class Declarations
[ June 19, 2002: Message edited by: Cindy Glass ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whew.... not quite sure if I understand the question... but it *sorta* sounds like you're looking for a local class, or an anonymous class
These are discussed a lot in the Programmer's Certification Study Forum -- do a quick search in there - I'm sure you'll find some great thread.
Check out this one, it seems promising: A class defined within a method
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops never thought on this..must be interesting..would like to come back after some trials..thanks for such a good thought...
Arun
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have classes inside a method but that class would only exist until the program exited the method.
No, the class can continue to exist afterwards - it just can't be referenced as that class from elsewhere in the code. But if a local class implements an interface, you can pass an instance of the local class off to some other object, and the instance can continue to exist, and be referenced as an instance of the interface, long after the method has completed execution.
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an example of what Jim's talking about, take a look at Vector's elements method:
 
reply
    Bookmark Topic Watch Topic
  • New Topic