• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum example from Java Language Specification does not compile.

 
Jim Newton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code, cut and paste from the JLS does not compile. The code is at the end of
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9.

The error I get from 1.6_0.21 is:

Operation.java:21: cannot find symbol
symbol : method eval(double,double)
location: class Operation
System.out.println(x + " " + op + " " + y + " = " + op.eval(x, y));

What's wrong?

Thanks,

Steve

------------------------------------------
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. You may have found an error in the specification. I get the same problem, and I don't think it's just down to the version of Java you're using (though it's possible).

Here's how I see what's going on. When you define a constant-specific class body like this, what I think is going on behind the scenes is that you're effectively creating a subclass of Operation. Then the constant is an instance of that subclass. But you're referencing it as if it was just a plain Operation, and eval doesn't exist in the Operation class.

But you can get it to compile, and work in the way they're intending, by adding the following line to the Operation classbody:
It ought to be abstract because if you provide a default implementation you don't force each constant to implement eval itself. And this breaks part of the point of the pattern - as the spec says:
...the pattern precludes the possibility of forgetting to add a behavior for a new constant (you'd get a compile-time error)

Which can be confirmed by adding a new constant (say POWER_OF) and not providing an implementation of eval. If the method is abstract then there's a compiler error. (I wasn't 100% sure this would work without marking the enum as abstract, but it does).


The spec also says
The above pattern is suitable for moderately sophisticated programmers
, so perhaps Beginning Java isn't the best place for this!


Oh, and welcome to JavaRanch . By the way, adding Code Tags would make your post much easier to read.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the "// abstract double eval(double x, double y);" line from the JLS should not be commented out. That commenting is probably a mistake.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, I completely failed to notice that commented out line! Ah, good, so I got to the right place even if it was by the scenic route .
 
Jim Newton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the swift solution!
And for the tip about code tags - I was wondering how to do that and have edited accordingly.

Steve
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic