Anamay Pandey
Anamay Pandey

Reputation: 11

ArrayList empty while accessing it in another class

  1. Prepared a toy class.

    public class toy {
    
      public String name;
      public int id;
      public int price;
    
      public toy(String name, int id, int price) {
          this.name = name;
          this.id = id;
          this.price = price;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public int getId() {
          return id;
      }
      public void setId(int id) {
          this.id = id;
      }
      public int getPrice() {
          return price;
      }
      public void setPrice(int price) {
          this.price = price;
      }
    }
    
  2. Prepared another class to hold as list of toys. Same class provides getter and setter methods for the list of toys.

    import java.util.ArrayList;
    public class accMeth {
    
        public static ArrayList<toy> toylist=new ArrayList<toy>();
    
        public ArrayList<toy> getToylist() {
            return toylist;
        }
    
        public void setToylist(ArrayList<toy> toylist) {
           this.toylist = toylist;
        }
    
    }
    
  3. This class below creates and adds toys in toyList. Then setting the toyList using setter.

     import java.util.ArrayList;
     import java.util.List;
    
     public class adding extends accMeth {
         public static void main(String[] args) {
    
             toy t1= new toy("gg",1,20);
             toy t2 = new toy("gg",2,23);
    
             accMeth meth=new accMeth();
    
             toylist.add(t1);
             toylist.add(t2);
             meth.setToylist(toylist);
             System.out.println(meth.getToylist().get(1).getPrice());
         }
     }
    
  4. Now if I want to access list, here it appears to be empty.

     public class getting{
    
         public static void main(String[] args) {
    
             adding ad= new adding();
             System.out.println(ad.getToylist().isEmpty());
         }
     }
    

Upvotes: 0

Views: 149

Answers (3)

Sideeg MoHammed
Sideeg MoHammed

Reputation: 369

it's empty because you fill it in an instance in 3 and printing from a new instance in 4

the main point is that public void setToylist(ArrayList<toy> toylist) { this.toylist = toylist; } you used this and it will add the value to this instant

if you want to use the static defend in 2 delete the this from the set method and access it as Static

public static void setToylist(ArrayList<toy> toylistar) {
toylist = toylistar;
}

now you marked the method as static and now you have to use it statickly

toy t1= new toy("gg",1,20);
toy t2 = new toy("gg",2,23);



 toylist.add(t1);
 toylist.add(t2);
accMeth .setToylist(toylist);

System.out.println(accMeth.getToylist().get(1).getPrice());


}

now you can find the change in the main

public static void main(String[] args) {

adding ad= new adding();
System.out.println(accMeth.getToylist().isEmpty());


}

but don't forget to put the add to contracter in number 3

import java.util.ArrayList; import java.util.List;

public class adding extends accMeth { public  adding () {

toy t1= new toy("gg",1,20);
toy t2 = new toy("gg",2,23);


 toylist.add(t1);
 toylist.add(t2);
accMeth.setToylist(toylist);

System.out.println(accMeth.getToylist().get(1).getPrice());


 }

Upvotes: 1

Tahmid Ali
Tahmid Ali

Reputation: 815

  1. Class Toy.java:

name, id and price variables should be private. Constructor should be Toy instead of toy

public class Toy {

    private String name;
    private int id;
    private int price;

    public Toy (String name, int id, int price) {
        this.name = name;
        this.id = id;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}
  1. Class accMeth:

Should be AccMeth. 'toylist' should not be public and static since, you have employed getter and setters. Plus, you will be modifying it. Check java Aceess Modifiers

public class AccMeth {

    private ArrayList toylist = new ArrayList();

    public ArrayList<Toy> getToylist() {
        return toylist;
    }

    public void setToylist(ArrayList<Toy> toylist) {
        this.toylist = toylist;
    }
}
  1. class adding

No need to extend the other class as it is in your code. I mean, you could directly access the ArrayList from the class that is the execution point. i.e. holds the main method.

public class Adding {

    public static void main(String[] args) {
        Toy t1 = new Toy("gg", 1, 20);
        Toy t2 = new Toy("gg", 2, 23);

        AccMeth meth = new AccMeth();
        ArrayList<Toy> toylist = meth.getToylist();
        toylist.add(t1);
        toylist.add(t2);
        meth.setToylist(toylist);

        System.out.println(meth.getToylist().get(1).getPrice());
    }
}

Upvotes: 1

tuantm
tuantm

Reputation: 19

you should move block code: "adding toy to list" to adding constructor

Upvotes: -1

Related Questions