From: Vikram on 2 Jul 2010 05:14 Hi, Consider the code snippet below: public Class A{ private int i = 10; public void execute(A anotherInst){ anotherInst.i = 20; } } We are able to access the private variable i of another instance of A. Why should this be allowed?
From: bugbear on 2 Jul 2010 05:20 Vikram wrote: > Hi, > > Consider the code snippet below: > > public Class A{ > > private int i = 10; > > public void execute(A anotherInst){ > anotherInst.i = 20; > > } > } > > We are able to access the private variable i of another instance of A. > Why should this be allowed? Because privacy is defined by class not instance, BugBear
From: Mayeul on 2 Jul 2010 05:22 Vikram wrote: > Hi, > > Consider the code snippet below: > > public Class A{ > > private int i = 10; > > public void execute(A anotherInst){ > anotherInst.i = 20; > > } > } > > We are able to access the private variable i of another instance of A. > Why should this be allowed? I guess a class is supposed to know whether it should mess with other instances the class than the current one. -- Mayeul
From: Patricia Shanahan on 2 Jul 2010 08:08 bugbear wrote: > Vikram wrote: >> Hi, >> >> Consider the code snippet below: >> >> public Class A{ >> >> private int i = 10; >> >> public void execute(A anotherInst){ >> anotherInst.i = 20; >> >> } >> } >> >> We are able to access the private variable i of another instance of A. >> Why should this be allowed? > > Because privacy is defined by class not instance, And has to be defined that way to allow e.g. an equals method that depends on private data or methods. Patricia
From: Lew on 2 Jul 2010 08:37
Vikram wrote: > Consider the code snippet below: > > public Class A{ > > private int i = 10; > > public void execute(A anotherInst){ > anotherInst.i = 20; > > } > } > > We are able to access the private variable i of another instance of A. > Why should this be allowed? Because of <http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.1> However, your example will fail to compile anyway. -- Lew |