Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Private object modifiable in other class  RSS feed

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following question from javablackbelt.com

What is the output of the following snippet?
01: import java.util.*;
02: class A {
03: private List lList = new ArrayList();
04: A(){
05: lList.add("hello");
06: }
07:
08: public List getList(){
09: return lList;
10: }
11:
12: public int getCount(){
13: return lList.size();
14: }
15: }
16:
17: class B{
18: public static void main(String args[]){
19: A a = new A();
20: List lList = a.getList();
21: lList.add("world");
22: System.out.println(a.getCount());
23: }
24: }

The output is 2
How come the reference lList in main method of class B change the object declared as private in class B.
Is that a loophole that we need to fix in our getter methods so as not to expose the private objects like this??
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How come the reference lList in main method of class B change the object declared as private in class B.



Hi,
I think you mean how can lList in class B change object declared as private in class A.
Well , this is the whole point of encapsulation.
-Make instance variables as private
-Have accessor and mutator methods access them. ie the get and set methods

Here the lList in class B HIDES the lList of class A.
Since lList in class A is declared as private , it is not visible to class B.
However, with line 20 they both point to the same list object in the heap.
Now you add "hello" in class A and "world" in class B
End result:-You have 2 objects in the list.
Hence the output 2
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As llist is private is it accessible to B. As far as i know, it is accesibel to particular class in which it is defined..
Please correct me if i am wrong...
 
Rajat Asani
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dinesh,
You are absolutely correct. The private instance variables are accessible only in the class they are declared in.

However, for this issue leave aside the private reference variable concept and try to look at it in OBJECT's perspective.

lList in class A points to one List.(lets call this lList as Alist)
lList in class B points to the same List on the heap.(lets call this lList as Blist)

Alist adds "hello" to the list on heap
Blist adds "world" to the same list on heap
Now you are calling getCount() method on the list. So you get value as 2.

Please do not confuse yourself with the same name (lList) in both the classes. Try the same example with the naming conventions i have used for explanation above.

Hope this helps....I know I am not good in explaining stuff
[ April 09, 2008: Message edited by: Rajat Asani ]
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what being private stops you from doing is this:



you cannot DIRECTLY access a's lList. However, you provided a getter that lets something else refer to it.

if you want to truly protect it, you probably shouldn't provide a getter, but implement some kind of add() method to your class A.

I may be wrong on all this, however...
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

lList in class A points to one List.(lets call this lList as Alist)
lList in class B points to the same List on the heap.(lets call this lList as Blist)



How do we know if lList in class B is pointing to the same List on the heap?
 
Rajat Asani
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Because of the following code we come to know that lList in class B is pointing to same list object on heap:-



This returns the reference of the List on heap and assigns it to lList in class B
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question now is how to avoid such a situation while developing some commercial project??
 
fred rosenberger
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't want someone to have access to your list, then don't write a method that gives them access to the list.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Fred Rosenberger:
if you don't want someone to have access to your list, then don't write a method that gives them access to the list.



or I can return the list from Class A as:

return (new ArrayList(lList));

that way, I will preserve my list in class A. The changes made by Class B will be to the dummy object.

This is also known as defensive copying.
Correct me if I am wrong!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!