Because private means private to a class, not an instance.  That's just the
way Java works.  See here for more:

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

chris

"Thomas Sundberg" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]...
> Hi!
>
> Could someone explain to me why this works? I has alway belived that
> private variables were just private to that particular instance, but I
> think this example shows that it is possible to reach the value of a
> private variable in another instance of the same class.
>
> Obviously I'm able to reach p2.name in the exmpale below from
p1.display().
> (I know beacues the class compiles and runs...)
> But I can't explain why at the moment.
>
> Anyone?
>
> public class Person {
>      private String name = null;
>
>      public Person(String namn) {
>          this.name = namn;
>      }
>
>      public void display(Person p) {
>          System.out.println(p.name);
>      }
>
>      public static void main(String[] args) {
>          Person p1 = new Person("Putte");
>          Person p2 = new Person("Quarolin");
>          p1.display(p2);
>      }
> }
>
> /Thomas
>


_______________________________________________
Eap-features mailing list
[EMAIL PROTECTED]
http://lists.jetbrains.com/mailman/listinfo/eap-features

Reply via email to