0

dog1.setName("rex") is not assigning the value "rex" to name, and is instead returning null why is this happening?

public class Program3a {
    private  String name;

    public  String getName() {
        return name;
    }      

    public void setName(String name) {
        name = "unassigned";
    }
}

public class Program3aTest extends Program3a{
    public static void main(String[]args) {
        Program3aTest dog1 = new Program3aTest();
        dog1.setName("rex");

        System.out.println(dog1.getName());

    }
}
forpas
  • 160,666
  • 10
  • 38
  • 76
user12114019
  • 57
  • 1
  • 8
  • 5
    `name = "unassigned";` should be `this.name = "unassigned";`. Otherwise you are modifying the parameter instead of the class field. – Ivar Oct 08 '19 at 15:18
  • 4
    And even then, the name will never be `"rex"` . – Arnaud Oct 08 '19 at 15:19
  • okay so I changed name = " unassigned" to this.name = "unassigned"; and now it is returning unassigned instead of null, but still not rex – user12114019 Oct 08 '19 at 15:24
  • 2
    @user12114019 That is what Arnaud meant. In your `setName` you set the `name` to `= "unassigned"`. So when you call it, it will do exactly that. – Ivar Oct 08 '19 at 15:26
  • Okay so how do I get the value to be rex? because when I remove the = "unassigned"; I receive the syntax error insert "Assignment operator expression" to complete expression. what do I do? – user12114019 Oct 08 '19 at 15:30
  • @user12114019 See my proposed duplicate. You pass `"rex"` as a parameter to your `setName` which expects that parameter (`(String name)`). So when it is called, the string `"rex"` will be put in the `name` parameter. So all you have to do is assign `name` (referring to the parameter) to `this.name` (referring to the class field). – Ivar Oct 08 '19 at 15:32
  • I changed it to public void setName(String n); name = n, and now it is working. thank you very much, Ivar, you are very helpful – user12114019 Oct 08 '19 at 15:39
  • @user12114019 That is one way. You can also leave it `public void setName(String name)` and then set it as `this.name = name;`. – Ivar Oct 08 '19 at 15:41

3 Answers3

1

You made a mistake in setting the name in class Program3a. There should be this.name = name instead of name = "unassigned". So kindly replace this statement. Because the initial value of name is null set by the constructor by default. You have to assign it to the parameter name which is an input from Program3aTest class. Here is your code displaying "rex".

public class Program3a {
    private  String name;

   /* public Program3a(String name) {
        setName(name);
    }*/

    public  String getName() {
        return name;
    }      

    public void setName(String name) {
        this.name = name;      // change this statement in your code
    }



}

public class Program3aTest extends Program3a{
    public static void main(String[]args) {
        Program3aTest dog1 = new Program3aTest();
        dog1.setName("rex");

        System.out.println(dog1.getName());

    }
}
Yahya
  • 33
  • 8
1

Change the Program3a class like so:

public class Program3a {
    private  String name = "unassigned";

    public  String getName() {
        return name;
    }      

    public void setName(String name) {
        this.name = name;
    }
}
Squti
  • 4,171
  • 3
  • 10
  • 21
1

In short your "getter" is returning what the variable value you are giving it -> string(type) variable name = "unassigned". You will always tell it to set the variable named "name" to the String unassigned. try this

public void setName(String name) {
    this.name = name;
}

Additionally you would want to create a constructor and make it overall more efficient (constructors can get rid of the setters and what not). In full:

public class Main {
    public static void main(String[] args){

    Program3a testClass = new Program3a();

    testClass.setName("Rex");
        System.out.println(testClass.getName());
    }
}

public  class Program3a {
    private  String name;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Or the better looking constructor

public Program3a(String name) {
    this.name = name;
}

this will require you to update your tester class of course (I use main).

    Program3a testClass = new Program3a("Rex");

    System.out.println(testClass.getName());

Constructors are automatically created as a ghost. This being overridden can take in the parameters of variables and reduce un-needed coding. Once this is created you use line Program3a testClass = new Program3a("Rex"); to create a new class. The Rex now auto fills the variable Named and continues to use this value when testClass.getName() is called until altered.