File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Switch Statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Switch Statements" Watch "Switch Statements" New topic
Author

Switch Statements

Albert Richards
Greenhorn

Joined: Apr 28, 2010
Posts: 7
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Apr 28, 2010
Posts: 7
Hi many thanks for the responses. I will look into what you have suggested.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Aug 16, 2005
Posts: 13868
    
  10

Ok Campbell, here's a slightly better version of the non-OO version:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch Statements
 
Similar Threads
Patterns for replacing Switching statements
Inheritance / Komposition of multiple abstract classes
SCJP for Java 6 book by Sierra and Bates covariant return code example question
Another switch statement query
Tokenize numbers only, skip words