Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Possible Errata for OCP Java SE 8 Study Guide

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ISBN 978-1-119-06790-0

549 page, Chapter 1 mock explanation, answer #19:
The answer is correct. There's a statement that D is compiling. Actually non-static nested classes may not have a static members.
 
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please copy the question for those of us who don't have that book.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.
They are accessed using the enclosing class name.
 
Eduard Gizatullin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell Ritchie, here are excerpts
19.jpg
[Thumbnail for 19.jpg]
19 question
 
Eduard Gizatullin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and answer
19a.jpg
[Thumbnail for 19a.jpg]
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot swap between instance methods and static methods. If the superclass' method is instance type, then the subclass' method must be instance type. If you redefine toddle() in the superclass as static, you will still have problems because inner classes don't have static members (except constants). Let's try it on JShell:-

Campbell's JShell wrote:public class LearnToWalk {
  ...>    public void toddle() {}
  ...>
  ...>    class BabyRhino extends LearnToWalk {
  ...>       public static void toddle() {}
  ...>    }
  ...> }
|  Error:
|  toddle() in LearnToWalk.BabyRhino cannot override toddle() in LearnToWalk
|    overriding method is static
|        public static void toddle() {}
|        ^----------------------------^


public class LearnToWalk {
  ...>    public static void toddle() {}
  ...>
  ...>    class BabyRhino extends LearnToWalk {
  ...>       public static void toddle() {}
  ...>    }
  ...> }
|  Error:
|  Illegal static declaration in inner class LearnToWalk.BabyRhino
|    modifier 'static' is only allowed in constant variable declarations
|        public static void toddle() {}
|        ^----------------------------^

jshell> public class LearnToWalk {
  ...>    public static void toddle() {}
  ...>
  ...>    class BabyRhino extends LearnToWalk {
  ...>       public void toddle() {}
  ...>    }
  ...> }
|  Error:
|  toddle() in LearnToWalk.BabyRhino cannot override toddle() in LearnToWalk
|    overridden method is static
|        public void toddle() {}
|        ^---------------------^

jshell> public class LearnToWalk {
  ...>    public void toddle() {}
  ...>
  ...>    class BabyRhino extends LearnToWalk {
  ...>       public void toddle() {}
  ...>    }
  ...> }
|  created class LearnToWalk

You were correct in your first post; A and C are correct overrides, The last code block I tried on JShell is equivalent to A and it compiled first time. B is a new method with a different name, and F is an overloaded method.
Neither E, which declares a “new” exception, nor D, which changes between instance and static type, will compile.
 
Eduard Gizatullin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, may be the book authors have some time and reason to change the explanation of D item


The next point to improve I suggest is in page 91. Chapter 2, paragraph 2

...we have declared these constructors with default or package-level access...


Seems it better to omits 'or'
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The official term is default access. Lots of people call it package access, package‑private access, or, here, package‑level access. I think the “or” is correct. It separates the two titles.
 
Eduard Gizatullin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks for explanation, this is some basics most of us are familiar with. But for those book readers who feel not so confident about the context, it sounds ambiguously, IMHO of course
 
Eduard Gizatullin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seemingly wrong answer for question 11 chapter 2
chapter2_mock_question11.jpg
[Thumbnail for chapter2_mock_question11.jpg]
 
Eduard Gizatullin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lambda of option D declares a local variable with the same name as parameter, it isn't compiled,  thus D is incorrect
chapter2_mock_explanation11.jpg
[Thumbnail for chapter2_mock_explanation11.jpg]
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look through the book's website; I couldn't find that error. I think you are right that you have found a “new” erratum (well done), but I haven't tried compiling the code.
I think the grammar of the sentence about package access is written on the assumption that readers are already familiar with the terms default access and package‑level access, and it could cause confusion.
 
author & internet detective
Posts: 39760
797
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's odd. Here's what's in my copy of the book for that answer/explanation.

What format is your book? Print? Kindle? Safari? Test questions website?

552.jpg
[Thumbnail for 552.jpg]
 
A berm makes a great wind break. And we all like to break wind once in a while. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!