Private Member Access Java

后端 未结 7 2216
盖世英雄少女心
盖世英雄少女心 2020-12-05 16:29

Is the private member access at the class level or at the object level. If it is at the object level, then the following code should not compile

    class Pr         


        
相关标签:
7条回答
  • 2020-12-05 17:28

    As others have stated, private, default access ("package private"), protected and perhaps in JDK 7 module are class based (there are very strange rules for nested classes inheritance that I can't remember). But why?

    Primarily it's down to methods that act as binary (or more) operators. For efficient implementation they often require or are easier to write without having to use or modify the public API. Have a look through at implementations of equals - in good code you'll find direct access of fields with few method calls to this. (The performance aspect of this is now mostly irrelevant with modern JVMs inlining common calls, but the code quality issue is still there.)

    0 讨论(0)
提交回复
热议问题