What is the use of a private static variable in Java?

前端 未结 19 2205
灰色年华
灰色年华 2020-12-02 03:47

If a variable is declared as public static varName;, then I can access it from anywhere as ClassName.varName. I am also aware that static members a

19条回答
  •  离开以前
    2020-12-02 04:36

    Well, private static variables can be used to share data across instances of that class. While you are correct that we cannot access the private static variables using constructs like ClassName.member or ClassInstance.member but the member will always be visible from methods of that class or instances of that class. So in effect instances of that class will always be able to refer to member.

提交回复
热议问题