What are the alternatives to public fields?

前端 未结 12 2667
刺人心
刺人心 2021-02-19 15:05

I am programming a game in java, and as the question title suggestions i am using public fields in my classes. (for the time being)

From what i have seen public fields a

12条回答
  •  暖寄归人
    2021-02-19 15:34

    private fields and setters and getters is indeed your best way to go.

    Further note that this is in general good code in any language as it keeps your security nice and tight while also giving you a structure that is far easier to debug and maintain. (Don't forget to document btw!)

    All in all, go with setters and getters, it's just good practice even if you find options.

提交回复
热议问题