r/javahelp • • Jul 23 '20

Solved Need help understanding objects

Hello, the following code is given off a practice exam for our upcoming exam:

public class Main {
    public static void main(String[] args) {
        Test t = new Test();
        t.i = 200;
        System.out.print(Test.i + " " + t.i);
    }
}
class Test {
    public static int i;
    public Test() {
        i = 100;
    }
}

The question is asking for the output. The output is: 200 200

Why? I understand why t.i is 200, but why is Test.i 200? From my understanding, Test is the 'type' of an object. Does this mean that the variable i will change for every object of type Test? Is this because i is static, public, or both?

18 Upvotes

Duplicates