Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from book OCA Oracle Certified Associate Study Guide

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question about Chapter 5 review question 20, I typed in the code exactly and it won't compile.
The book says it should compile and output "Bird is flying". But, I get this compiler error:
Bird.java:4: error: non-static variable this cannot be referenced from a static context
Bird bird = new Pelican();

Here is the code:

 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran your code on my machine, it is working perfectly fine.
 
Greenhorn
Posts: 13
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I ran the exact code that you posted and got the same error message.

It's a simple syntax error.

On Line 6 and Line 7, there should have been 2 closing curly braces; on Line 10, only 1 closing curly brace.

Cheers!

 
Marshal
Posts: 79530
379
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Benedict Wong wrote:. . . On Line 6 and Line 7, there should have been 2 closing curly braces; on Line 10, only 1 closing curly brace. . . .

Well done finding that

That shows the danger of writing incorrectly indented code. Let's add some code tags to your code. Always use the tags: you will see how much better the code looks soon
Look at lines 2 and 8. You have crammed a whole method into one line; it shou‍ld occupy three or four lines, with appropriate spacing. Maybe the book had code in that format; some certification examples use incorrect indentation to confuse the candidates. But incorrect indentation has converted the subclass into something different, which cannot be instantiated as it stands. I shall let you read the link rather than trying to explain it all here. Had you been aware of the indentation, you wouldn't have had that compiler error. One aid to correct indentation is what I call writing backwards.
 
Benedict Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: some certification examples use incorrect indentation to confuse the candidates.



I totally concur with you on this, Campbell, and I think I'll do well to learn from the way you pay attention to all the details.  

While a set of code might be syntactically correct, it does not mean the presentation conforms to best practices. Moreover, since Java is a free-form language, I think learning to adhere to proper style is all the more important. Something along the lines of Java Style Guide.

So I guess the exams give us an opportunity to train our eyes and learn to see through the "bad form"/booby traps and understand the logic behind.

Cheers!
 
Campbell Ritchie
Marshal
Posts: 79530
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quite a good style guide, from Apache. One slide mentions a default constructor, which I think is there by mistake. Another mentions member variables prefixed m_ which worries me.
 
Bari Clark
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I typed it in exactly as presented in the book. That is why stuff is all crammed together.
I am studying to take the OCA exam, so I guess it is good practice to get used to the kind of horrible stuff they throw at you.

Thank you all so much for helping me find out what was wrong. I was already confused about method hiding, and the error in the book made it that much harder to figure out. Having this forum was a great help!
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic