Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 4, Question 13 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ray Kolbe
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm a bit confused about the explanation to question 13 in chapter 4. The book says there are 4 compilation errors but I believe there to be 3. I think the confusion comes from the book's explanation "Line 4 doesn't compile because bench is not set in either of these locations." Locations in this case are "declaration line" or a "static initialization block".

You don't have to set bench to anything and the code compiles just fine:



The problem is really with line 15 where we first try to assign a value to the static final member. This is accounted for in the answer.

Perhaps I'm missing something obvious and/or I'm approaching this code sideways

Any help is appreciated. Thanks!
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try removing 3 lines (11,12,15). If you are correct and there are only 3 compilation errors, then the code should compile as is.

See what the compiler says about line 4 when you do!
 
Ray Kolbe
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh. Ok. I think there was something funky going on with my machine or I was just having a moment.

I went back to my test code and rewrote the example and now it fails compilation the way you would expect.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ray Kolbe wrote:Huh. Ok. I think there was something funky going on with my machine or I was just having a moment.

I went back to my test code and rewrote the example and now it fails compilation the way you would expect.


Does that mean you understand why it fails to compile for 4 reasons? I could elaborate but I think you got it now. Good luck studying!
 
Ray Kolbe
Greenhorn
Posts: 19
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I understand now. Thanks Scott!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ray Kolbe,

First of all, a warm welcome to CodeRanch!

Ray Kolbe wrote:I'm a bit confused about the explanation to question 13 in chapter 4. The book says there are 4 compilation errors but I believe there to be 3. I think the confusion comes from the book's explanation "Line 4 doesn't compile because bench is not set in either of these locations." Locations in this case are "declaration line" or a "static initialization block".

You were definitely not the only one who was confused about this question Here and here you'll find explanations about exactly the same question. Although you already seem to understand what's wrong, it might be worth to read those topics as well (to confirm your thoughts maybe ). And if it is not useful anymore for you, it probably will be useful for other ranchers who stumble upon this topic because they were confused about this question too.

Hope it helps!
Kind regards,
Roel
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Questions like these make a great case for why it's often better to study for the exam without an IDE like Eclispe. The IDE might only report the first or second compilation error, whereas the code may have 3-4 compilation errors. As Roel points out in other posts, fixing one error might then show other errors. Since you can't take the exam with an IDE, you need to understand the code and what's wrong with it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic