Skip to content
CodeKruPro

A static counter shared by all objects

Predict the outputEasyJava onlyAsked in: Infosys
Main.java
class Counter {
    static int count = 0;
    int id;

    Counter() {
        count++;
        id = count;
    }
}

public class Main {
    public static void main(String[] args) {
        Counter a = new Counter();
        Counter b = new Counter();
        Counter c = new Counter();
        System.out.println(a.id + " " + b.id + " " + c.id);
        System.out.println(Counter.count);
    }
}

Graded by exact match. Spaces at the ends of lines and blank lines at the end do not count.

to answer and keep your progress. Reading is free for everyone.