If there are 4 objects of type StaticExample, how many different instances of x are there?

If there are 4 objects of type StaticExample, how many different instances of x are there?

a)       0
b)       1
c)       3
d)       4
e)       There is no way to know since any of the objects might share x, but they do not necessarily share x

Use the following class definition:
                                          public class StaticExample
                                          {
                                                        private static int x;
                                                       
                                                        public StaticExample (int y)
                                                        {
                                                                      x = y;
                                                        }

                                                        public int incr( )
                                                        {
                                                                      x++;
                                                                      return x;
                                                        }
                                          }

Answer:  b.

Explanation:  Because x is a static instance data, it is shared among all objects of the StaticExample class, and therefore, since at least one object exists, there is exactly one instance of x.


Enhancing Classes

Learn More Multiple Choice Question :