Tuesday, 10 June 2014

Example to prove that static each class object has the same static member(single copy of static member is maintained).

class Diff
{
static int m = 0;
int n = 0;
Diff()
{
  m = m+1;
  n = 10;
  }
public static void main(String...args)
{
  Diff ob1 = new Diff();
Diff ob2 = new Diff();
  System.out.println("Non static 1:"+ob1.m);
  System.out.println("Non static 2:"+ob2.m);
  System.out.println("Static 1:"+ob1.n);
  System.out.println("Static 2:"+ob1.n);
}
}

No comments:

Post a Comment