The Artima Developer Community
Sponsored Link

Programming in Scala Forum
"A class and its companion object can access each other’s private members."

3 replies on 1 page. Most recent reply: Tues, Aug 13 3:18 AM by mart stub

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
    Next Topic
Flat View: This topic has 3 replies on 1 page
Twan van der Schoot

Posts: 1
Nickname: 80043
Registered: Dec, 2011

"A class and its companion object can access each other’s private members." Posted: Apr 23, 2016 5:01 AM
Reply to this message Reply
Advertisement
In the PDF version of the 3rd edition of Programming in Scala in §4.3 2nd paragraph last sentence says:

"A class and its companion object can access each other’s private members."

This is in its generality false. A class can see (private) members of a companion object, but a companion object can't access the members of the associate class.

Moreover, you need to import the companion object's member names explicitly using e.g. "import X._" before you can access the object's members from the associated class or from an instance of that class.

But I fail to see how an object can have meaningful access to class members, which are essentially always members of an instance. If there is more than one instance, which instance to choose?

1. I think this line should be corrected in the book because it is very confusing. Only if you know Java and the behavior of a static in a class well enough, you understand why it can't work.

2. I failed to find an explanation in the book never that you need to import the names from the object into the class, before you can access them. It would have been nice to have that pointed out in the book.

At this moment only googling and experimenting (and my experience with Java) made clear what the limitations on the purported mutual accessibility of (companion) object and class members actual are.


Daniel Barclay

Posts: 5
Nickname: daniel7
Registered: Sep, 2016

Re: "A class and its companion object can access each other’s private members." Posted: Nov 27, 2016 7:42 AM
Reply to this message Reply
You might want to also report that via the errata pages at
http://booksites.artima.com/programming_in_scala_3ed/errata.

zivep max

Posts: 2
Nickname: zivep
Registered: Mar, 2019

Re: "A class and its companion object can access each other’s private members." Posted: Mar 27, 2019 3:15 AM
Reply to this message Reply
How to download Emus4u Without Jailbreak on Emus4u

mart stub

Posts: 1
Nickname: martstub
Registered: Aug, 2019

Re: "A class and its companion object can access each other’s private members." Posted: Aug 13, 2019 3:18 AM
Reply to this message Reply
Nice information and you explained in a detailed way. Thanks for sharing this to us.
expresshr is one of the best online survey conducted by kroger.

Flat View: This topic has 3 replies on 1 page
    Next Topic Topic: 5 Tips to Get More Accurate Results on Google Search


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use