Last active
February 26, 2018 05:55
-
-
Save amrza/a1d90ca887bccfba309699560cde551a to your computer and use it in GitHub Desktop.
Objects of the same type will have access to each others private and protected members even though they are not the same instances! WTF!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Person.class | |
//------------------------- | |
class Person { | |
private String name; | |
public Person(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public void changeName(Person p, String newName) { | |
p.name = newName; // the problem is here! | |
} | |
} | |
// App.class | |
//------------------------- | |
public class App { | |
public static void main(String[] args) { | |
// We create two Person! | |
Person john = new Person("John"); | |
Person mary = new Person("Mary"); | |
// Ok, its fine. it will print: John | |
System.out.println(john.getName()); | |
// Ok, its fine. it will print: Mary | |
System.out.println(mary.getName()); | |
// How this can happen when mary's name is private? | |
john.changeName(mary, "Susan"); | |
// What the f*ck? how can john change mary's name? | |
System.out.println(mary.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to the Specification of java :
when a class variable access modifier defines as "private" this parameter is still accessible within the body of that class. Now look at this part :
p.name = newName; // the problem is here!
Since this p.name has been declared within the body of the Person class the rule of restriction isn't enforced.
Take a look at this link :
https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.1
Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.
I'm agree that having the access modifier must be enforced within object-level not class-level BUT I'm pretty sure that there are other scenarios like encapsulating an "static inner class" for creating builder pattern will get affected by the means of this restriction so I do believe that the team of Java has come up with a right way of enforcement and productivity of the compiler design and after all they come up with this particular criteria.
Anyways Java still Rocks and this scenario is duplicating within lots of other modern and well-known programming languages now a days.