• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Enum example from Java Language Specification does not compile.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

------------------------------------------
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22683
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76440
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic