Variables in Interface

前端 未结 5 2000
长情又很酷
长情又很酷 2020-12-11 02:05

Why is that a variable used in an Interface is PUBLIC STATIC FINAL? Why \"static\" in particular?

5条回答
  •  孤街浪徒
    2020-12-11 02:31

    An interface is a contract that defines the interaction between objects.

    This interaction is defined by the exposed methods, not by the variables. Variables would only describe the internal working, not the interaction.

    Note that variables should never be used for interaction. According to the OOP principle of encapsulation, it would be a crime to let 1 class access a variable of another class directly.

    Constants (e.g.Math.PI) are the only acceptable exception. Since constants are the only kind of variables that can be accessed directly by other classes without violating the principle of encapsulation, all variables in an interface are treated as public static final variables (i.e. constants)

提交回复
热议问题