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

前端 未结 7 1064
被撕碎了的回忆
被撕碎了的回忆 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:06

    Actually the HotSpot compiler would most likely inline calls to your getters, so no overhead will be involved (besides, the overhead for calling these methods would hardly be measurable).

    EDIT

    If you really need every CPU cycle, use C or C++ (or write performance-critical parts in it and call it via JNA, though its unlikely that it will be worth the time spent).

提交回复
热议问题