About Question enthuware.ocajp.i.v7.2.1363 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1363 :

Post by Sweetpin2 »

This question has one answer like below

you cannot change a String object, once it is created.

Is the above statment correct?

String str1 = "old String";
str1 = "new String";

isn't String object str1 got changed from old String to new String??

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by admin »

No, just the str1 reference got changed. Earlier it was pointing to a String object containing "old String", and after the assignment it started pointing to another String object containing "new String". The old String object was left unchanged.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

baxhuli
Posts: 5
Joined: Wed Mar 27, 2013 11:57 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by baxhuli »

That's because it is final, not because it is immutable. You can have a final class whose objects are mutable.
This is in the explanation below the fourth choice. Can you give an example of a final class whose objects are mutable?
Thank you.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by admin »

baxhuli wrote:
That's because it is final, not because it is immutable. You can have a final class whose objects are mutable.
This is in the explanation below the fourth choice. Can you give an example of a final class whose objects are mutable?
Thank you.
You can mark any class as final without affecting its mutability. I.e. if that class was mutable earlier, it will remain mutable.

A final class means it cannot be subclassed. That's it. It has nothing to do with the mutability (i.e. whether you can change the contents of its objects or not) of the class. For example,

Code: Select all

public final class TestClass { //<-- This class is mutable with or without final.
  public String str; //You can make str point to different String objects. Thus, TestClass is mutable.
}
But this class is immutable:

Code: Select all

public class TestClass { //<-- This class is non-mutable with or without final.
  public final String str = "hello"; //You cannot make str point to any other string. Thus, TestClass is immutable.
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

baxhuli
Posts: 5
Joined: Wed Mar 27, 2013 11:57 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by baxhuli »

So for a class to be not mutable, it has to have its members declared final so that you can not change them? It makes sense now. Thank you very much :)

rocoty
Posts: 10
Joined: Tue Dec 01, 2015 11:22 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by rocoty »

I would argue that a class cannot be immutable without being final. A subclass could easily introduce new mutable state to an otherwise immutable class. And thus, I would say as a consequence of being immutable, a class cannot be extended, because it cannot be truly immutable unless it is final.

Thoughts?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by admin »

If a subclass introduces a mutable state then that subclass becomes mutable. Why do you think that makes the original parent class mutable? Can you change the state of the objects of the parent class?
If you like our products and services, please help us by posting your review here.

rocoty
Posts: 10
Joined: Tue Dec 01, 2015 11:22 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by rocoty »

admin wrote:If a subclass introduces a mutable state then that subclass becomes mutable. Why do you think that makes the original parent class mutable? Can you change the state of the objects of the parent class?
If that object is most specifically an object of the parent class, then no obviously not.

But imagine the following scenario:
class Foo is non-final but otherwise immutable, and is documented as such.
class Bar extends Foo, introduces new mutable state.

Say I write a method that takes one Foo instance. I'd expect all instances passed to this method to be immutable. After all, they're all instances of the immutable class Foo. However, since Foo is not final, and Bar extends Foo, the Foo instances passed to my method could very well be Bar objects. Any instance of Bar is also an instance of Foo. Bar is a mutable class, and so the objects my method receive may not all be fully immutable, seeing as some of them could actually be Bar objects.

Does my reasoning make sense, or have I completely misunderstood mutability?

EDIT: To clarify, I'm arguing that an immutable class should not be able to be extended. This does not necessarily mean it has to be final, as you could achieve this using only constructors that are not accessible to any potential subclass. My point still stands in the context of this question, though.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by admin »

Your reasoning makes sense but there is one flaw- the method that receives a Foo object can only call methods or access fields of Foo class. It cannot call methods and access fields (which may be mutable) of Bar class. So it doesn't really matter to your method if the object passed to it is of Bar class if the method doesn't even know about the existence of Bar class. Therefore, there is no cause of concern in your method if you pass an object of some other subclass that is mutable.
i.e.
void method(Foo f){
f.barMethod(); //can't do this
f.barField = 10;//can't do this
f.fooField = 10;//can't do this because fooField is final.
}

So you can still expect that Foo objects passed to the method are immutable. Their "bar" properties may not be immutable but you don't expect that anyway because Bar is indeed not immutable.



HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Kevin30
Posts: 28
Joined: Sun Oct 25, 2015 10:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by Kevin30 »

To shine light for myself on this subject matter, I have come to the following summarization (reading the enthuware explanations & discussion forums):

String, StringBuilder and StringBuffer are final classes.
String class is immutable, which means that its members are declared final.
StringBuilder class and StringBuffer class are mutable, which means that its members are not (always) declared final.
String - StringBuilder - StringBuffer.JPG
String - StringBuilder - StringBuffer.JPG (32.32 KiB) Viewed 8045 times
Am I correct?

This might also explain why some methods in the StringBuilder class mutate the StringBuilder object (such as append(), insert(), delete(), replace()) while other methods in the StringBuilder class do not mutate the StringBuilder object (such as toString() and substring()).

The only thing is that if I read the String methods or StringBuilder methods in the Java API (which I am admittedly not very familiar with reading), I can't really see any String methods and StringBuilder methods that are mentioned as final methods.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by admin »

You are mostly correct. But all methods of a final class are implicitly final. There is no need to declare them so.
If a class cannot be extended its methods cannot be overridden, right?

Second thing is that finality of a method has no bearing on immutability of a class. A class can be immutable even if its methods are not final.
-Paul.
If you like our products and services, please help us by posting your review here.

earlisreal
Posts: 1
Joined: Tue Jun 21, 2022 6:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by earlisreal »

I also want to argue about immutable class not marking as final, the Study Guide by Scott Selikoff and Jeanne Boyarsky says that the first rule "Mark the class as final or make all of the constructors private." (Sybex OCP 17 Study Guide chapter 6, Creating Immutable Objects section). It is just confusing for the readers of that book. Otherwise the reasoning from the admin also makes sense for me.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1363 :

Post by admin »

I don't have the complete statement fron that book but what you have copied here is definitely incorrect. You don't need constructors to be private for a class to be immutable. A class can be immutable even if it is not final and has public constructors.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 43 guests