java - working of static in following code -


class test {     static int x = 11;     private int y = 33;     public void method(int x) {         test t = new test();         this.x = 22;         y = 44;         system.out.println(test.x);         system.out.println(t.x);         system.out.println(t.y);         system.out.println(y);     }     public static void main(string args[]) {         test t = new test();         t.method(5);     } } 

here code ok , gives output like: 22 22 33 44

but don 't understand:

  1. why test.x isn't 11 , give output like: 11 22 33 44

  2. why t.y , y isn't 44 , 44 , gives output like:11 22 44 44

  3. why t.method(5) not executed in code?

the difference between static field , instance field static field not instance-dependent.

look here:

static int x = 11; private int y = 33; 

the static field x same every instance of test while field of y instance specific. that's why see these results.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -