Home » JAVA Programming » Modifier Types » Question
  1. What is the output of this program?

    class modifier
    {
    static int p;
    void method()
    {
    p++;
    }
    }
    public class static_keyword
    {
    public static void main(String args[])
    {
    modifier obj1 = new modifier();
    modifier obj2 = new modifier();
    obj1.p = 5;
    obj1.method();
    obj2.method();
    System.out.println(obj1.p + " " + obj2.p);
    }
    }
    1. 9 9
    2. 8 8
    3. 7 7
    4. 6 6
    5. 5 5
Correct Option: C

All objects of class share same static variable, all the objects share same copy of static members, obj1.p and obj2.p refer to same element of class which has been incremented twice and its value is 5.



Your comments will be displayed only after manual approval.