0

I have 3 java programs with thee different variables used in it gives same output. But i can't get the logic around the variables used in the inside and outside the programs. so please help if you got time.

1

class A {
    static String name1;

    A(String name1) {
        this.name1=name1;
    }
}

public class Nam extends A {
    Nam(String name1) {
        super(name1);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

2

class A {
    static String name1;
    A(String a) {
        this.name1=a;
    }
}


public class Nam extends A {
    Nam(String name1) {
        super(name1);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

3

class A {
    static String name1;
    A(String a) {
        this.name1=a;
    }
}

public class Nam extends A {
    Nam(String p) {
        super(p);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}
Matt Cutts
  • 11
  • 4

1 Answers1

0

1

class A {
    static String name1; //static field "name1" of type String

    A(String name1) { //constructor of "A" with a String parameter
        this.name1=name1; //pass the value of the parameter "name1"
                          //to the static field "name1"
    }
}

public class Nam extends A {
    Nam(String name1) { //constructor of "Nam" with a String param
        super(name1); //call the constructor of "A" with the parameter
                      //passed to this constructor ("name1" 1 line above)
    }

    public static void main(String args[]) { //main function of the app
        Nam ob=new Nam("hai"); //create 1 object of Type "Nam" passing
                               //to the "Nam" constructor the string "hai"
        System.out.println(name1); //print the value of the static field "name1"
    }
}

2

class A {
    static String name1;
    A(String a) {
        this.name1=a; //same as in nr 1, only now the local value
                      //that is passed is called "a" inside the constructor
    }
}


public class Nam extends A {
    Nam(String name1) {
        super(name1);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

3

class A {
    static String name1;
    A(String a) {
        this.name1=a;
    }
}

public class Nam extends A {
    Nam(String p) { //again, the local name changed to "p"
                    //it only matters inside this constructor
        super(p);
    }

    public static void main(String args[]) {
        Nam ob=new Nam("hai");
        System.out.println(name1);
    }
}

Q: In the third one how the value given to p in class Nam reflect on the different variable a in class A

A: Exactly the same way it does in nr 1 and nr 2. In nr 3 it's called p, but it could be anything, as long as it is the same in the function heading and body, for example:

Nam(String functionParameter) {
    super(functionParameter);
}

Step by step, it looks like this:

  1. Nam ob=new Nam("hai"); calls the Nam constructor with the string "hai"
  2. Nam(String functionParameter) { so now functionParameter becomes a reference to "hai"
  3. super(functionParameter); call the A constructor with the string "hai"
  4. A(String a) { and now a becomes a reference to "hai"
  5. this.name1=a; finally, the static field a becomes a reference to the string "hai"