Do we have a Readonly field in java (which is set-able within the scope of the class itself)?

前端 未结 7 1225
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 01:31

How can we have a variable that is writable within the class but only \"readable\" outside it?

For example, instead of having to do this:

Class          


        
7条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 02:16

    from what I know the compiler does not optimize this kind of code

    The Hotspot JIT compiler most definitely does.

    I was wondering what's the best solution?

    Stop optimizing prematurely. If you're using this code in a painting routine, I can guarantee that the actual painting will take at least a hundred times longer than calling a trivial method, even if it's not inlined.

提交回复
热议问题