logic in java constructor around different variables -


i have 3 java programs thee different variables used in gives same output. can't logic around variables used in inside , outside programs. please if got time.

1

class {     static string name1;      a(string name1) {         this.name1=name1;     } }  public class nam extends {     nam(string name1) {         super(name1);     }      public static void main(string args[]) {         nam ob=new nam("hai");         system.out.println(name1);     } } 

2

class {     static string name1;     a(string a) {         this.name1=a;     } }   public class nam extends {     nam(string name1) {         super(name1);     }      public static void main(string args[]) {         nam ob=new nam("hai");         system.out.println(name1);     } } 

3

class {     static string name1;     a(string a) {         this.name1=a;     } }  public class nam extends {     nam(string p) {         super(p);     }      public static void main(string args[]) {         nam ob=new nam("hai");         system.out.println(name1);     } } 

1

class {     static string name1; //static field "name1" of type string      a(string name1) { //constructor of "a" string parameter         this.name1=name1; //pass value of parameter "name1"                           //to static field "name1"     } }  public class nam extends {     nam(string name1) { //constructor of "nam" string param         super(name1); //call constructor of "a" parameter                       //passed constructor ("name1" 1 line above)     }      public static void main(string args[]) { //main function of app         nam ob=new nam("hai"); //create 1 object of type "nam" passing                                //to "nam" constructor string "hai"         system.out.println(name1); //print value of static field "name1"     } } 

2

class {     static string name1;     a(string a) {         this.name1=a; //same in nr 1, local value                       //that passed called "a" inside constructor     } }   public class nam extends {     nam(string name1) {         super(name1);     }      public static void main(string args[]) {         nam ob=new nam("hai");         system.out.println(name1);     } } 

3

class {     static string name1;     a(string a) {         this.name1=a;     } }  public class nam extends {     nam(string p) { //again, local name changed "p"                     //it matters inside constructor         super(p);     }      public static void main(string args[]) {         nam ob=new nam("hai");         system.out.println(name1);     } } 

q: in third 1 how value given p in class nam reflect on different variable in class

a: same way in nr 1 , nr 2. in nr 3 it's called p, anything, long same in function heading , body, example:

nam(string functionparameter) {     super(functionparameter); } 

step step, looks this:

  1. nam ob=new nam("hai"); calls nam constructor string "hai"
  2. nam(string functionparameter) { functionparameter becomes reference "hai"
  3. super(functionparameter); call constructor string "hai"
  4. a(string a) { , a becomes reference "hai"
  5. this.name1=a; finally, static field a becomes reference string "hai"

Comments