Monday, 5 October 2015

All Objects share the same copy of a static variable and Static variables are stored on method area


public class Acceptt {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Accepttt.k++;
  Accepttt.display();
  Accepttt obj1 = new Accepttt();
  Accepttt obj2 = new Accepttt();
  obj1.k++;
  obj1.display();
  obj2.display();
 }

}


class Accepttt {
 static int k=10;
static void display()
{
System.out.println("The value of k is " +k);
}
}


Output:-
The value of k is 11
The value of k is 12
The value of k is 12


No comments:

Post a Comment