Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Possible errata in OCA OCP Java SE 8 Programmer Practice Tests  RSS feed

 
Evgeniy Mamaev
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello JavaRanch!

Please, help me check myself. I'm confused by question number 2 in chapter 20 (Java Concurrency).

I see here a final keyword before AtomicInteger ticketsTaken. And afterwards there is a consistent changing of that variable. I was convinced the final instance member is not allowed to be changed after its initialization in the constructor.

So, my confusion is about aforementioned answer. Why nevertheless the correct option is B? As far as I understand, we can change AtomicInteger variable, despite it is marked as final. Do I catch it correct?

Thank you in advance for your explanation.

Sincerely,

Evgeniy
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Evgeniy Mamaev wrote:So, my confusion is about aforementioned answer. Why nevertheless the correct option is B? As far as I understand, we can change AtomicInteger variable, despite it is marked as final. Do I catch it correct?

If a reference veriable is marked final, you can't assign a new object to it.But you can still change the state of the object by invoking mutator methods on that reference variable.So that's definitely not an errata!
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to Roel, final "primitive values" are constant, like int/double/boolean.  On the other hand, final applied to an Object only refers to the reference variable.  The reference is constant, but the contents of the object are certainly not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!