• 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

Switch Statements

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, I am learning Java, and I would like to be able to do something.

I understand that switch statements allow for scenarios to be account for. I am trying to develop something in an object oriented manner, with abstract classes.

Could someone explain to me how I would be able to use a switch statement in the abstract class, then refer back to it at a later date.

I am looking for something that could do something like:

Case 1: return 10
case 2: return 20
case 3: return 30

then in another class return this value so I can add it to something else.

I hope I have been descriptive enough for someone to help me. Many thanks in advance!
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, put the code in a method then call that method from your code. Since you want it in an abstract class you would need a non-abstract subclass to inherit the method. Then you just call the method as you would call any other method.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Switch statements by themselves don't have anything to do with being object oriented. In fact, you could argue that using switch statements in some cases is going against OO principles; you can replace the switch statement with subclasses and overridden methods in those cases.

For example:
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree a "switch" is procedural programming. Particularly since the values after case must be of restricted range of types (at present int only, but the chars Jesper showed are implicitly cast to ints), and must be compile-time constants, since the switch block is set up at compile time.

But even a very object-oriented language like Eiffel (Object-Oriented Software Construction, by Bertrand Meyer, 1997(?)) supports something indistinguishable from a switch.

But shouldn't that performOp method be declared static, and the class be given a private constructor?
 
Albert Richards
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi many thanks for the responses. I will look into what you have suggested.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I agree a "switch" is procedural programming. Particularly since the values after case must be of restricted range of types (at present int only, but the chars Jesper showed are implicitly cast to ints), and must be compile-time constants, since the switch block is set up at compile time.

But even a very object-oriented language like Eiffel (Object-Oriented Software Construction, by Bertrand Meyer, 1997(?)) supports something indistinguishable from a switch.

But shouldn't that performOp method be declared static, and the class be given a private constructor?



Can't you use enum values in case statements?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

W. Joe Smith wrote: . . . Can't you use enum values in case statements?

Yes, you can. I forgot about that. Of course enum members are "read-only" so they are a sort of compile-time constant too.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Campbell, here's a slightly better version of the non-OO version:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic