To illustrate Changing the Reference
and Changing the value of member if Reference
, I tried with User defined class , posting here what I have tried and the observation -thanks @SteveP
//Case 1 Trying to Change Reference
Boolean[] array1 = new Boolean[5];
Arrays.fill(array1, Boolean.FALSE);
for(Boolean value : array1) { // Warning here The value of the local variable value is not used
value = Boolean.TRUE;
}
System.out.println(" Elements ==> "+array1[0]+" - "+array1[1]);
this will print Elements ==> false - false , Reference will not be able to modify
Case 2 Trying to Change Reference with user defined class
MyBool[] array3 = new MyBool[5];
MyBool boolInst2=new MyBool( Boolean.FALSE);
MyBool boolNew=new MyBool( Boolean.TRUE);
Arrays.fill(array3,boolInst2 );
for(MyBool value : array3) { // Warning here The value of the local variable value is not used
value = boolNew;
}
System.out.println(" Elements ==> "+array3[0].flag+" - "+array3[1].flag);
this will print Elements ==> false - false, Reference will not be able to modify
Case 3 Changing the values of members of an object (MyBool.value),
MyBool[] array2 = new MyBool[5];
MyBool boolInst=new MyBool( Boolean.FALSE);
Arrays.fill(array2,boolInst );
for(MyBool value : array2) {
value.flag = Boolean.TRUE;
}
System.out.println(" Elements ==> "+array2[0].flag+" - "+array2[2].flag);
this will print Elements ==> true - true , Values are updated
class MyBool{
public Boolean flag;
public MyBool(Boolean flag){
this.flag=flag;
}
}