java – why assigning instance variable inside method of implemented interface does not work while calling it from another local methods of the same class?-ThrowExceptions

Exception or error:

I have a class called “A” implements interface “I” like below

public interface I<T>{
   void setList(List<T> list)
}

public class A implements I<M>{
   private List<M> mList;

   public void load(){
      //Working on mList....
   }

   public void method(){
      someList;
      I<M> i = this;
      i.setList(someList);
   }

   public void setList(List<M> list){
      this.mList = list;
   }

}

public class M{
   private String str;
}

I want to assign value of list to mList and use it wherever inside the class however the value will be assigned inside the overriden method but when I use mList in any other methods like load it does not give me the value.

How to solve:

Finally I could find the solution I was supposed to do like below

public interface I<T>{
   void setList(List<T> list)
}

public class A implements I<M>{
   private List<M> mList;
   I<M> i;

   public A(){
      this.i = this;
   }

   public void load(){
      //Working on mList....
   }

   public void method(){
      someList;
      i.setList(someList);
   }

   public void setList(List<M> list){
      this.mList = list;
   }

}

public class M{
   private String str;
}

Leave a Reply

Your email address will not be published. Required fields are marked *