What is the use of package level protection in java?

前端 未结 15 1100
时光说笑
时光说笑 2020-12-05 14:28

I know how package level protection in java works. I read a lot of code (including lots of open source stuff) and no-one seem to be using it. The whole protection l

15条回答
  •  感动是毒
    2020-12-05 14:37

    Another use of default protection in Java is for performance.

    Inner classes are allowed to access private members of their containing class, but this is implemented through obfuscated accessor methods. Before the JIT compiler optimizes this away (or in environments without a JIT such as J2ME) this causes a small performance hit. Declaring the needed methods with default protection eliminates the need for these accessor method calls, but doesn't require making the members fully public.

    Keep in mind that this is only important where performance is known to be critical, and where you can't count on the JVM fixing the issue for you. But at least it doesn't really harm readability/maintainability as much as many other micro-optimizations.

提交回复
热议问题