Difference between public static and private static variables

前端 未结 5 1762
星月不相逢
星月不相逢 2020-12-02 15:07
class Employee{
 // salary  variable is a private static variable
private static double salary;

// DEPARTMENT is a constant
 public static final String DEPARTMENT =         


        
5条回答
  •  囚心锁ツ
    2020-12-02 15:26

    public static - can be accessed from within the class as well as outside the class.

    private static - can be access from within the class only.

    Static's are considered to be anti-OO in OOPS.

    class Dog
    {
        public static string X;
        private static string y;
    }
    

    y can be accessed only from inside Dog via either Dog.y or just y. X can be accessed anywhere via Dog.X or, if you're either in the class or using using static Dog as a header, just X.

提交回复
热议问题