Are global static classes and methods bad?

前端 未结 10 1289
予麋鹿
予麋鹿 2020-12-02 13:36

It\'s generally agreed upon that relying heavily on global stuff is to be avoided. Wouldn\'t using static classes and methods be the same thing?

10条回答
  •  不思量自难忘°
    2020-12-02 14:01

    First, why are the old global variables so bad? Because it is state that is accessible from anywhere, any time. Hard to track.

    There are no such problems with static methods.

    That leaves static fields (variables). If you declared a public static field in a class, that would truly be a global variable and it would be bad.

    But make the static field private and most problems are solved. Or better, they are limited to the containing class and that makes them solvable.

提交回复
热议问题