Java Encapsulation Concept not clear

后端 未结 12 2386
死守一世寂寞
死守一世寂寞 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:44

    It seems you are running two different classes separately and assuming the changes done to attributes when you run EmployeeTest will reflect in Employee run. Note that changes will reflect in the same JRE instance. Excuse me in case i have misunderstood your problem.

    EDIT: As per the user input. Here is the code how you can access and update the static member values:

    class Employee {
        public static int empid;
        public static String empname;
    
        public static void main(String[] args) {
            System.out.println("print employe details:" + empid + " " + empname);
        }
    }
    
    // EmployeeTest class
    public class EmployeeTest {
    
        public static void main(String[] args) {
            Employee e = new Employee();
            e.empid = 20;
            e.empname = "jerry";
            Employee.empid = 10;
            Employee.empname = "tom";
            Employee.main(null);
        }
    
    }
    

提交回复
热议问题