String is always immutable.
You are getting confused with local variable and instance variable.
nirjari patel wrote:
public void nameTest(String sName){
sName = sName + " idea ";
//start();
}
In this case variable sName is local variable which does shadow the instance variable sName. As
sName is local variable in this case scope of it is limited only for that method and instance vaeiable sName still pointing to "good", hence the result
Output : good
----------------
nirjari patel wrote:
Now when I am using name in place of sName in nameTest(), sName output is changing as follows
public void nameTest(String name){
sName = name + " idea ";
//start();
}
In this case local variable name is
name and you are assigning value to instance variable i.e.
sName hence the
Output : good idea
---------------------