Every time I create a new Dog object on a main Java class, I am unable to replace “null” (which represents the String Name variable from the Dog class) with the string I want. Here is the Class:
private String Name;
private int Age;
public Dog(String Name, int Age) //Constructor {
this.Name =(String) Name;
this.Age = Age;
}
public int getAge() {
return Age;
}
public void setAge(int Age) {
this.Age = Age;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
Try
Dog dog1 = new Dog("James", 2);instead. The string literal has to be enclosed in double quotes, else, it’ll be treated as a variable.or, add
before instantiating the Dog with
Dog dog1 = new Dog(James, 2);