non-static vs. static function and variable

前端 未结 4 1525
無奈伤痛
無奈伤痛 2021-02-04 12:55

I have one question about static and non-static function and variable.

1) non-static function access static variable.

It\'s OK!

class Bar
{
publ         


        
4条回答
  •  情歌与酒
    2021-02-04 13:57

    Static means this is independent of a particular instance of the class. Static methods don't have access to the this pointer. That is the reason you need to call them using the class name.

    When you call the Static method, you might not even have any instance of the class defined.

    non-static means implies an instance, and could be different with different instances.

    So, basically, it does not make sense to access non-static members from static methods.

提交回复
热议问题