Java Encapsulation Concept not clear

后端 未结 12 2395
死守一世寂寞
死守一世寂寞 2020-11-30 03:40

This is basic question but still i don\'t understand encapsulation concept . I did\'t understand how can we change the properties of class from other class.because whenever

12条回答
  •  孤独总比滥情好
    2020-11-30 03:43

    public static field's are associated with class not with object, it break Object's encapsulation rule.

    Employee class with two encapsulated field empid & empname.

    public class Employee {
        private int empid;
        private String empname;
    
        public int getEmpid(){
            return this.empid;
        } 
        public void setEmpid(int empid){
            this.empid = empid;
        }
        ...
    }
    
    public class EmployeeTest {
          public static void main(String[] args) {
                Employee e = new Employee();
                e.setempId(1);
                Employee e1 = new Employee();
                e1.setempId(2);
          }
    }
    

    Documentation for better understanding on encapsulation

提交回复
热议问题