Menu
Question Index
...

What will be the output of the following program?

class W
{
    static int c = 0;

    public static void main(String[] args)
    {
        W w1 = c();
        W w2 = c(w1);
        W w3 = c(w2);
        W w4 = c(w3);
    }

    private W()
    {
        System.out.println("c = " + c);
    }

    static W c()
    {
        return c++ <= 0 ? new W() : null;
    }

    static W c(W w)
    {
        return w.c++ == 1 ? new W() : null;
    }
}


c = 1
c = 2
c = 1
c = 2
c = 3
c = 1
c = 2
c = 3
c = 4
Compilation Error
NullPointerException

Doubts

Problems

Topic: Static Keyword In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App