Why can't I initialize my static data member in my constructor

后端 未结 5 1936
無奈伤痛
無奈伤痛 2020-12-15 05:28

I read the answer in parashift but I need bit details as to why compiler won\'t allow to define static member variable in constructor.

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 06:09

    static variable can't be defined inside any method (even if the method is static) you can however define the static variable outside the constructor and assign a value inside the constructor. But by doing so the variable will then be accessible to the whole class.

提交回复
热议问题