• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question in K&B Chapter 8

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 662 of SCJP book there is a question
1. public class TestObj {
2. public static void main(String[] args) {
3. Object o = new Object() {
4. public boolean equals(Object obj) {
5. return true;
6. }
7. }
8. System.out.println(o.equals("Fred"));
9. }
10. }

And the answers are
...
E. Compilation fails because of an error on line 4.
F. Compilation fails because of an error on line 8.
G. Compilation fails because of an error on a line other than 3, 4, or 8.

The correct answer given is G on the basis that compilation fails due to a missing ; on line 7 but I would have thought that this would be picked up by the compiler as a missing ; on line 8?
Equally you could say it was an error on line 3 because the assignment started on line 3 was not closed correctly.
Is there a standard way of knowing how to place errors on questions like this on the exam?
 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you notice line 3, you can see that an anonymous inner class is being declared that extends class Object. Line 7 is the end of that inner class. Line 8 doesn't belongs to the inner class discussed above. The assignment started at line 3 is not closed correctly on line 7. So, the compiler shows the error at line 7.
 
Joss Armstrong
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But when you try to compile this code you get the error:

TestObj.java:8 ';' expected
System.out.println(o.equals("Fred"));
^
1 error



So line 8 is the logical answer to this question
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the way the compiler tells about missing semicolons, So the semicolon is expected at the beginning of the next line following the statement that miss the semicolon.
[ September 18, 2007: Message edited by: Ahmed Yehia ]
 
yeah, but ... what would PIE do? Especially concerning this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic