• 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

Object Created inside switch

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Code Ranch peeps;

I'm very new to Java and this is my very first post within the coderanch and was wondering if any of you could assist me with the issue I'm encountering,

To explain my issue I have create as short example which contains two classes Cat and Dog followed by a switch statement in the main method.

Class Cat



Class Dog




Main Method




My Issue is that after creating the objects within the switch statement, I am not able to access them within my main method - is there a way to return the objects back to the main method once created within the switch ? or am I going about this to wrong way ?

would really appreciate some help








 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to declare the reference outside the switch so it doesn't fall out of scope.

you probably want both Dog and Cat to extend Animal or Pet or Mammal or something...so you'd then do this:

 
Dan Dowe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant, thank you for your time

reply
    Bookmark Topic Watch Topic
  • New Topic