Here is an excerpt from the JDK7 source code:
public String(String original) { this.value = original.value; this.hash = original.hash; }
Both valueand hashis a field private. Why original.valuelegal?
value
hash
private
original.value
See in this table :
Access Levels Modifier | Class | Package | Subclass | World -------------+--------+-----------+-----------+-------- public | Y | Y | Y | Y protected | Y | Y | Y | N no modifier | Y | Y | N | N private | Y ← | N | N | N
Access modifiers describe access for classes , not instances . Since it valuewas declared in a class String, all its elements, like constructors, have unlimited access to it.
String
( , this).
this
Both value and hash are private filed. Why is original.value legal?
String, .
Modifier Class Package Subclass World --------------------------------------------- private **Y** N N N
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
A private modifier indicates that an item can only be accessed in its class.
Since it is in the classroom, you can access it.
Getters and seters are valid for implementation outside the scope of the class. Inside the class, you can get any variable without any restrictions.
Source: https://habr.com/ru/post/1524454/More articles:Эмма для инструмента WAR файла - warJQuery ui tabs using class selector - jqueryFind a route from angularjs rubeprovider by name - angularjsconvert numpy string array to int array - pythonWhat is the difference between the U prefix for a character literal versus a string literal? - c ++Benefits of Yeoman, Grunt, and Bower for Rails - ruby-on-railsTDD with Sitecore 7 and TFS 2012 CI - unit-testingBeautifulSoup - how do I get the contents of a body - pythonPhpstorm does not work after upgrading version of TortoiseSVN to version 1.8 - svnjson.dumps () not working - jsonAll Articles