Topics Index

Static Block972

If you need explanation Read this topic

If you need Answer Take test on this topic

Static Block972
What will be the output of the following program?
class Increment
    public static void main(String[] args)
        Simple s = new Simple();
        Simple r = new Simple();


class Simple
    static int a = 5;
    void incr()

    void display()
        System.out.println("a = " + a);

A. a = 5
a = 5
B. a = 6
a = 6
C. a = 7
a = 7
D. Compilation Error
E. Runtime Error
Topic: Static Keyword In Java

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 C,,,bcoz a is static type variable both object create and use its only one copy,,, and incrmnt same varieble two time,,,

Posted by Raju VarshnEy    2014-11-14 12:32:40

Ans is we Simple class in that we have varible named with a

here a is an static variable which can be accessed with the class name and throug object also....static variables are common to all Objects....
When you create n num of objects than n num of copies will create for instance varibles...where as static variables create once and common to n num of objects....

here we create 2 objects for the simple class than here memory allocated for the a
here a is common to both the objects s and r

next we are calling the incr method with object s here we are increasing a value so that it wil be


next we are calling the incr method with object r here we are increasing a value so that it wil be

next we are calling display() with both objects so that it will print 7...

Posted by Uday Kumar    2014-11-14 12:43:13


Posted by Dereje Goshu    2014-11-14 12:43:24

ans c.this program explains usage of static variable.The problem is that Java doesn't allow global variables.                                                                    Every variable in Java must be declared inside a class. So Java uses the static keyword to indicate that a
particular variable is a class variable rather than an instance variable.                                                                                       The one copy of the variable exists regardless of the number of instances of the class that are created--it
exists and can be used even if the class is never actually instantiated.                                                          static variable can be initialized ly once .                                                     coming to this program we have 2 classes .                               in main class reference for simple class object is created s and r.                                                                            by using that reference incr() method is called.inside simple class a is declared with static assigned with 5 .so it get global access.                                                                     s.incr() method post incrementing a value so a=6,                                                                                      r.incr()--post increments a,a=7.                                                 bcz both s and r refernce to the same address location. after 2 incr() cal finaly a value becom 7.thn s.disply() sop prints 7,r.display() sop prints also 7.

Posted by Maheshwari Natarajan    2014-11-14 17:19:40

This dose is now closed and the winners are Raju VarshnEy,  for 'First Correct Comment', Uday Kumar, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-17 02:18:18

© meritcampus 2019

All Rights Reserved.

Open In App