Menu
Topics Index
...
`

Static Block


If you need explanation Read this topic

If you need Answer Take test on this topic

Static Block
What will be the output of the following program?
class SamsungMobile
{
    public static void main(String[] args)
    {
        Simple.getData();
        Simple.display();
    }
}
class Simple
{
    static int a, b = 20;
    static void getData()
    {
        System.out.println("a = " + a);
    }
    static void display()
    {
        System.out.println("b = " + b);
    }
    static
    {
        System.out.println("Samsung Mobile");
        a = 4 * b;
    }
}

A. Samsung Mobile
a = 0
b = 20
B. Samsung Mobile
a = 80
b = 20
C. a = 0
b = 20
D. Compilation Error
E. Runtime Error
Topic: Creating Static Methods In Java Using Static Keyword

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is B,static intializer gets executed when the class is loaded.

Posted by Raviteja Daggupati    2014-02-18 12:44:44


ans:B static variable are initialised only once and prints after all calls completed..

Posted by Karthik Narahari    2014-02-18 12:59:48


ans is B: static block will be executed first and static values persist their values through out the program

Posted by Vijay Kumar    2014-02-18 14:08:17


Congratulations Raviteja Daggupati. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-19 03:52:37


© meritcampus 2019

All Rights Reserved.

Open In App