aspose file tools*
The moose likes Beginning Java and the fly likes Enum example from Java Language Specification does not compile. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enum example from Java Language Specification does not compile." Watch "Enum example from Java Language Specification does not compile." New topic
Author

Enum example from Java Language Specification does not compile.

Jim Newton
Greenhorn

Joined: Feb 22, 2011
Posts: 11
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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Oct 27, 2005
Posts: 19783
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Feb 22, 2011
Posts: 11
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

Joined: Oct 13, 2005
Posts: 40034
    
  28
. . . and welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum example from Java Language Specification does not compile.