Page 1 of 1

About Question enthuware.ocajp.i.v8.2.875 :

Posted: Wed Aug 03, 2022 5:10 pm
by Omranhariri
:cheers:

Re: About Question enthuware.ocajp.i.v8.2.875 :

Posted: Wed Aug 03, 2022 5:13 pm
by Omranhariri
class Data {

int intVal = 0;
String strVal = "default";
public Data(int k){
this.intVal = k;
}

}

public class TestClass {
public static void main(String[] args) throws Exception {
Data d1 = new Data(10);
d1.strVal = "D1";
Data d2 = d1;
d2.intVal = 20; // is here a new Objekt has been created?!
System.out.println("d2 val = "+d2.strVal);
}
}

Re: About Question enthuware.ocajp.i.v8.2.875 :

Posted: Wed Aug 03, 2022 9:51 pm
by admin
>d2.intVal = 20; // is here a new Objekt has been created?!

No. intVal is a primitive field of the existing object of class D2 pointed to by the variable d2. This field is being assigned a new int value of 20 here.