Answers ( 2 )

  1. 2017-01-06 10:01

    Since a in Class A is private it should always be accessed via some method of A.

    class A{
        private int a=0;
        A(){
            B b = new B();
            b.set(this);
    
        }
    
        public void change(int f){
            a= f;
        }
    }
    
    class B{
        void set(A x){
            x.change(5); // or any pass value
        }
    }
    
  2. 2017-01-06 10:01

    You must write getter and setter method in class A of a private variable.

     class A{
          private int a=0;
          public int getA() {
            return a;
        }
        public void setA(int a) {
            this.a = a;
        }
        A(){
             B b = new B();
          }
        }
        class B {
            public int b = 0;
          void set(){
            A a = new A();
            b = a.getA();
          }
        }
    
◀ Go back