How come setWav doesn’t change the wav in wav + " " + "Grapes"? Do I have to create a set method for add if I want it to output Oranges? Grapes?
EDIT: Thanks everyone for the help, much appreciated.
package javaapplication1;
public class JavaApplication1 {
public static void main(String[] args)
{
NewClass object1 = new NewClass();
System.out.println(object1.getAdd());
object1.setWav("Oranges?");
System.out.println(object1.getAdd());
}
}
OUTPUT:
Burgers Grapes
Burgers Grapes
package javaapplication1;
public class NewClass
{
private String wav;
private String add;
public NewClass()
{
wav = "Burgers";
add = wav + " " + "Grapes";
}
public void setWav(String wav)
{
this.wav = wav;
}
public String getWav()
{
return wav;
}
public String getAdd()
{
return add;
}
}
Your
getAddmethod returnsadd. You set the value ofaddin theNewClassconstructor but nowhere else, hence it remains unchanged even when you changewav(these two variables are not related in any way, changing one will not effect the other). You can try this: