Java Magazine, Jan/Feb 2018
Its prohibited for an overriding method to declare checked exceptions that were not already permitted in the context of the overridden method ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2018 108 fix this In option C there is a single field int x The field is private but not final The value of the field is initialized with a copy of the value passed to the constructor All arguments in nonremote Java method invocations are passed by value and with primitive types the value really is the value being represented not the value of the reference Because of this changes to the original variable that was passed as an argument to the constructor do not afect the value of x Also no code in the class ever changes the value of x after the object is constructed So even though the field is not marked final instances of this class are immutable and option C is correct In option D you again see a private nonfinal field This time its List String ls Because its private and nothing outside the class ever has a copy of the reference value in ls nothing will ever change the contents of the list or point the variable at a diferent list Therefore option D is correct Option E is a little more subtle You have a variable ls which is identical to the one described in option D Therefore you know that nothing changes the value of ls to make it refer to a diferent list object If you can be sure that the list that ls refers to cannot be altered in any way you would know the object is immutable The variable ls is initialized to refer to a list created by the Arrays asList method which is a utility that describes itself as creating a structurally immutable list which sounds promising the list will not allow the addition or removal of elements However the list created by Arrays asList actually honors attempts to reassign any given element of the list But to counter that this list is wrapped in Collections unmodifiableList which puts a proxy wrapper around the object so that any attempt to modify the list will throw an exception Surely this must be immutable right Well it turns out that the list thats created uses the provided array as its backing storage Therefore if the caller of the constructor chooses to provide an explicit String as its argument any changes made to that array will be reflected in the list Because of this the objects are not reliably immutable and option E is incorrect
You must have JavaScript enabled to view digital editions.