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

Need Explanation: Reference Variable encapsulation

 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refering to book "Sierra K., Bates B. - OCA OCP Java SE 7 Programmer I & II Study Guide - 2015" Chapter 05 page 295, says that

"When encapsulating a mutable object like a StringBuilder, or an array, or an ArrayList, if you want to let outside classes have a copy of the object, you must actually copy the object and
return a reference variable to the object that is a copy. If all you do is return a copy of the original object's reference variable, you DO NOT have encapsulation."

Code is like:




Could anyone provide a bit explanation of this?

Thanks.
 
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
Kaleem Anwar wrote:Could anyone provide a bit explanation of this?

Have a look at this topic. It has some excellent explanations with a few illustrative code snippets.

If you still have doubts/questions after reading this thread, just let us know by replying to this thread.

Hope it helps!
Kind regards,
Roel
 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel, really helpful explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic