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

前端 未结 5 1717
刺人心
刺人心 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 12:44

    Static methods do not apply to a particular instance/object, they're a class-level thing. Because of that, they're not given an instance reference. So, no you cannot do this.

    If you can work out which instance reference to use another way, you could access the non-static methods of it.

    Or, alternatively, re-architect your classes so that it's a non-static method.

提交回复
热议问题