how to access a non static member from a static method in java?

前端 未结 5 1709
刺人心
刺人心 2020-12-22 12:35

I have a situation where i have to access a non static member from inside a static method. I can access it with new instance, but current state will be lost as non static me

5条回答
  •  春和景丽
    2020-12-22 13:01

    A non static member variable "is state". It's the state of a specific instance of that class.

    When you say you want to access a non-static member variable, it's as good as saying "want to access a non-static member variable of a specific instance of class XXX", I mean the bolded part is implicit.

    So it doesn't make sense to say "I can access it with new instance".

提交回复
热议问题