Exercise (Solutions)

E1

Consider the following class:

public class TypesOfVariables {
    public static int x = 7;
    public int y = 3;
}

What are the class variables?

What are the instance variables?

E2

Study the code below and evaluate the expected output before running the code..

class VariableTest {
    public static void main(String[] args){
        TypesOfVariables a = new TypesOfVariables(); 
        TypesOfVariables b = new TypesOfVariables(); 
        a.y = 5; 
        b.y = 6; 
        a.x = 1; 
        b.x = 2; 
        System.out.println("a.y = " + a.y); 
        System.out.println("b.y = " + b.y); 
        System.out.println("a.x = " + a.x); 
        System.out.println("b.x = " + b.x); 
        System.out.println("TypesOfVariables.x = " + TypesOfVariables.x);
    }
}

public class TypesOfVariables {
    public static int x = 7;
    public int y = 3;
}

Run the code and justify the reasoning for the values you see in the println statements

results matching ""

    No results matching ""