static variable vs non static variable

前端 未结 4 869
遥遥无期
遥遥无期 2020-12-20 08:37

I have defined an object and declared a static variable i. In the get() method, when I try to print the instance and class variable, both print the

4条回答
  •  甜味超标
    2020-12-20 09:32

    The field i is declared as static. You can access static fields either with the YourClass.StaticField or instance.StaticField. So both of

    this.i
    test.i
    

    are referring to the same value in the context of an instance method of your test class.

    It's considered bad practice to access a static field with this.i or instance.i.

提交回复
热议问题