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

前端 未结 19 2251
灰色年华
灰色年华 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:28

    Static variables have a single value for all instances of a class.

    If you were to make something like:

    public class Person
    {
        private static int numberOfEyes;
        private String name;
    }
    

    and then you wanted to change your name, that is fine, my name stays the same. If, however you wanted to change it so that you had 17 eyes then everyone in the world would also have 17 eyes.

提交回复
热议问题