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

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

    Your solution is: private fields, private setters, protected or public getters (note: protected allows access from same package as well as from subclasses)

提交回复
热议问题