I'm having a problem with Java. Why does the following code return null
?
public class TestClass {
public String top,top1,top2,top3,top4;
public TestClass(){
changeTop(top);
changeTop(top1);
}
public void printTop(){
System.out.println(top);
}
public void changeTop(String reference){
reference="lolly";
}
}
When I run the following:
TestClass x = new TestClass();
x.printTop();
It also returns null
. I don't understand it because i thought I was passing a reference to the ChangeTop
.
How can I fix this? I want to give the name of the parameter to a method to change it.