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 by String not possible, so why does Java tutorial say it is?

 
Matt Hoover
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20184
26
MySQL Database
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20184
26
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matt Hoover
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic