-3

I am not getting updated list from a method passed a references

    List<Person> beans = new ArrayList<Person>();
    boolean alreadyPresent = isPersonPresentOnSolr(solrServer, beans);

    // beans.size() is zero
    boolean isPersonPresentOnSolr(SolrServer solrServer, List<Person> beans)
   {
    QueryResponse response   = solrServer.query(solrQry);                
    beans = response.getBeans(Person.class);

    //beans.size()  is 5
   }
ManojP
  • 6,113
  • 2
  • 37
  • 49
  • 3
    not enought information. Use debugger. – jhamon Nov 27 '14 at 09:54
  • I am passing empty list to a method as parameter and populating it inside method. I am able to see updated list inside method using debugger but getting empty list outside method. – ManojP Nov 27 '14 at 09:56
  • http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – cy3er Nov 27 '14 at 10:00

1 Answers1

1

you should not do

beans = response.getBeans(Person.class); // you lost reference of object. it is c++ way,
//but not works for java

you should do

List<Person> newBeans = response.getBeans(Person.class);
beans.addAll(newBeans);
Adem
  • 9,402
  • 9
  • 43
  • 58
  • but I am able to see same list updated inside method and loose all reference once pointer goes out from method – ManojP Nov 27 '14 at 09:58
  • as I explained, it is c++ way. this is not pointer exactly. think about, "main beans" points 0x1000 address. in "function beans", first, it points 0x1000 address too. then you call response.getBeans. and it creates a new object and points 0x1001. and you assign it to "function beans". so "function beans" points 0x1001. when you look it in debugger, you see 5 items in 0x1001, because "function beans" points 0x1001. but "main beans" still points 0x1000. so, you need to copy elements from 0x1001 to 0x1000, or you need to change address "main beans" pointed to 0x1001 – Adem Nov 27 '14 at 10:10
  • Thanks Adem for details info. – ManojP Nov 27 '14 at 10:40