0

Lets say you have those methods :

public static void listAdd(List<Integer> list){
    list.add(123);
    list.add(123);
    list.add(234);
    

}
static void compute(String s){
    switch(s){
    case "1" : s ="other";
    break;
    case "test" : s ="2222";
    break;
    default : s ="none";
    
    }
    System.out.println("my value inside is none:" + s);

}

Then inside my main i will call both of them:

    listAdd(list);
    System.out.println(list.size());
    String var = "helloworld";
    compute(var);
    System.out.println(var);

Why does size return 3 [inserted 3 elements changed the list] but my string doesnt change?

3 (outside static) none (system out inside static) helloworld (outside static)

  • Oh yes of course list being List list = new ArrayList(); listAdd(list); – julainoCewb Feb 11 '21 at 13:12
  • If you want to return a value from a method, you need to return it. In java, there isn't such thing as output parameters (there are in some other languages). – Amongalen Feb 11 '21 at 13:13
  • 1
    Might be worth reading this: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Amongalen Feb 11 '21 at 13:14

0 Answers0