aspose file tools*
The moose likes Beginning Java and the fly likes switch by String not possible, so why does Java tutorial say it is? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "switch by String not possible, so why does Java tutorial say it is?" Watch "switch by String not possible, so why does Java tutorial say it is?" New topic
Author

switch by String not possible, so why does Java tutorial say it is?

Matt Hoover
Greenhorn

Joined: May 01, 2011
Posts: 21
In current version of Java, I understand switch by String is not possible (though will be in future version). So why does the current Java tutorial say it is?

http://download.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).

I am working on an assignment for a "factory class" (see below) and trying to find the cleanest "switcher type" to use. Currently I'm using int, which works but does not communicate intent at all (the calling method just gives argument of 1 or 2 which doesn't describe the shape it's asking for at all). String would be desirable obviously, but it doesn't work. I'm assuming the Java tutorial is NOT incorrect in saying Enum works? I could see Enum working here for me, but when I try to implement it I can't quite get it to work. If I define the enum in the ShapeFactory class, not sure how to use that data-type outside of the factory class (i.e. in the class which is calling the factory method and needs to give it an enum type). Does this make sense?

Thanks!

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Because the tutorials have been updated to reflect the rules of Java 7. Unfortunately Java 7 hasn't been released yet, so that was a little premature (and confusing), but that's how it is.

To answer your other question, yes, you can use the members of an Enum class as switch constants.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
To be fair, that same tutorial page does have this:
Using Strings in switch Statements

In Java SE 7 and later, you can use a String object in the switch statement's expression.
The implication here is that it is not possible in earlier versions of Java. And Java SE 7 is available now as a preview release, here. It's not unreasonable for someone learning Java now to try that version and learn about some of the new features.
Matt Hoover
Greenhorn

Joined: May 01, 2011
Posts: 21
Paul Clapham wrote:To answer your other question, yes, you can use the members of an Enum class as switch constants.

Ok, thanks. But I am still stuck when trying to implement it this way. If I define the enum inside the ShapeFactory class, how can I use that enum as an argument to the (external) call to the ShapeFactory getObject method?

For example, if I define inside my ShapeFactory class:

enum aShape { LINE, CIRCLE };

Then how can I call ShapeFactory.getObject(LINE) in another class, since that other class won't know about the enum? I'm probably overlooking something obvious, but I would love some help with that. Thanks again!
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Does it need to be contained by the class? If not you could just create a separate enum:

then import it where you need it.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Matt Hoover wrote:If I define the enum inside the ShapeFactory class, how can I use that enum as an argument to the (external) call to the ShapeFactory getObject method?

For example, if I define inside my ShapeFactory class:

enum aShape { LINE, CIRCLE };

Then how can I call ShapeFactory.getObject(LINE) in another class, since that other class won't know about the enum? I'm probably overlooking something obvious, but I would love some help with that. Thanks again!


First you have to declare the enum in such a way that it can be accessed from another class. Declaring it public would work; there are other options but let's leave them aside for now since you're posting in Beginning Java. Then you refer to it as a static member of ShapeFactory, like this:


However declaring it on its own, as Michael Ernest suggests, is just as good. (It took me a while to realize you could do that, which is why I know the answer to this question.)
Matt Hoover
Greenhorn

Joined: May 01, 2011
Posts: 21
Thanks Paul, I was really close to that solution already but I forgot to label the enum as static. It actually crossed my mind about 5 minutes before I read this post, but reading it here was good reinforcement. :-)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

If the enum is relevant to the factory class only, why not move the creation of the shapes to the enum?
Instead of calling ShapeFactory.getObject(ShapeFactory.aShape.LINE) you now call ShapeFactory.aShape.LINE.getObject().

That does make me question if the ShapeFactory class is still relevant; if this is the only code it has (which may not be the case, you may have just removed the rest to keep the post short) then all it does is provide an enclosing class around the aShape enum. You might as well turn it into a package instead if the name space is important, or leave it out completely.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Hoover
Greenhorn

Joined: May 01, 2011
Posts: 21
Hmm, I did not even know you could use methods inside enums like you are, nor do I fully understand all the details of what you're doing there.

But, I got it working the other way (using ShapeFactory.getObject(ShapeFactory.aShape.LINE)), so that is good enough for now. Thanks for the suggestion though.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Enums are much like regular classes. You can add fields, methods, and even constructors (although the latter must be private). The enum itself can also have abstract methods, but in that case each enum constant must override that method. Overriding of non-abstract non-final methods is of course also allowed.

In my example, the enum can be considered to be the equivalent of an abstract class. Each constant is an instance of an anonymous sub class. This is also the only form of sub classing allowed for enums - anonymous sub classes per constant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch by String not possible, so why does Java tutorial say it is?
 
Similar Threads
Switch statement - Constant expression
Doubt in Rules Roundup switch int only (enum works)
switch case problem
does switch take a Integer wrapper?
What does (int) mean?