I have the following code that prints 20.
import java.util.*;
import java.lang.*;
import java.io.*;
class A {
public int val = 20; // <- no static
}
class Main {
public static void main(String[] args) {
A a = new A();
updateVal(a);
System.out.println(a.val);
}
private static void updateVal(A a) {
a = new A();
a.val = 50;
}
}
But if I change val to static then it prints 50. How it works?
import java.util.*;
import java.lang.*;
import java.io.*;
class A {
public static int val = 20; // <- there is static
}
class Main {
public static void main(String[] args) {
A a = new A();
updateVal(a);
System.out.println(a.val);
}
private static void updateVal(A a) {
a = new A();
a.val = 50;
}
}