From: Vikram on
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
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
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
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
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