File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch Statements

 
Albert Richards
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • 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:
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Campbell, here's a slightly better version of the non-OO version:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic