Surya Rawat Surya Rawat - 3 months ago 6
Java Question

What should be output of below program?

it's bit silly but I am confused with below problem, output is def. But I am not understanding, what is happening inside show method.

public class TestStatic {

public static String s="abc";

static
{
s="def";
}

public static void show()
{
s="ghi";
}

public static void main(String[] args) {
System.out.println(TestStatic.s);

}

}

Answer

It will be

def

since you are never calling show() and the static block is called before the main method is.

Comments