java - Scope and lifetime of List -


i have static list of products type.when populate list shopowner class, works fine, when compile customer.java , list returns blank set. why populated list not retaining ?

class products {      string name;     int itemcode;     products(){}     static list <products> list = new arraylist<products>();     products(string name,int itemcode)       {           this.name=name;         this.itemcode=itemcode;       }     public string tostring()     {return (name+""+itemcode);}         }  class shopowner {          public static void main (string ...at)  {         products o = new products("shamppo",12);             products.list.add(o);         products o1 = new products("choco",1112);            products.list.add(o1);            system.out.println(products.list); //prints fine }     }   class customer {                 public static void main (string args[])             {                   system.out.println(products.list);   //prints []             }     } 

output (when compiling customer.jav)

  []  

see below part of code

public static void main (string ...a) { products o= new products("chocolate");  o.addtolist(); products o1= mew products("icecream"); o1.addtolist(); new products().showlist(); //this line culprit 

you adding list in object o , o1 , calling showlist() on different object altogether new products().showlist();?

note: works static list because shared among objects of product class, not case non-static list.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -