• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Object Created inside switch  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 12746
51
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Brilliant, thank you for your time

 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!