Yes still learning the basics in Java! So the text I am referring to says - reference types (all except the primitives) are copied by reference. Now, I am also told Strings are reference types - which is why -
String str = "test";
String another_str = "test";
if ( str != another_str )
{
System.out.println("Two strings are different"); // this is printed
}
If that is the case, why does this happen -
package prj_explore_infa_sdk;
public class parent_class {
public String test;
public parent_class (String str){
test = str;
test = "overriden";
}
public parent_class (){
test = "overriden";
}
}
package prj_explore_infa_sdk;
public class child_class extends parent_class{
public static void main(String[] args){
child_class cls = new child_class();
if ( cls instanceof parent_class)
System.out.println("Subclass is an instanceof parent class");
String start = "nice";
parent_class pclass = new parent_class(start);
start = "changed";
System.out.println("Start : " + start + ", pclass.test : " + pclass.test);
}
}
This prints -
Subclass is an instanceof parent class
Start : changed, pclass.test : overriden
Why isnt this changing the member test of the pclass object, if the String reference was copied?