tag:blogger.com,1999:blog-5638372.post8168979762825997631..comments2023-10-10T05:22:56.347-05:00Comments on binkley's BLOG: Immutable objects in JavaBrian Oxleyhttp://www.blogger.com/profile/06617364377560752378noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-5638372.post-7616645591947522562007-11-10T07:33:00.000-06:002007-11-10T07:33:00.000-06:00Mikko, this is certainly true. I just prefer the ...Mikko, this is certainly true. I just prefer the copy-constructor idiom from C++ for the purpose I have in mind. A matter of taste, to be sure. In non-example code I would have describe this idiom in the javadocs for the sake of those not familiar with it.Brian Oxleyhttps://www.blogger.com/profile/06617364377560752378noreply@blogger.comtag:blogger.com,1999:blog-5638372.post-32778449319714448932007-11-10T06:05:00.000-06:002007-11-10T06:05:00.000-06:00Because it is not static field, new object do not ...Because it is not static field, new object do not have it set. If purpose is to have new instance with same species, but with new length, following should work:<BR/>// Snakes can grow but cannot change species<BR/><I><BR/>DontTreadOnMe setLength(int length) {<BR/> return new DontTreadOnMe(this.species, length);<BR/>}<BR/></I>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5638372.post-5993807706103073432007-11-07T05:16:00.000-06:002007-11-07T05:16:00.000-06:00Mikko, what about the field, 'species'?Mikko, what about the field, 'species'?Brian Oxleyhttps://www.blogger.com/profile/06617364377560752378noreply@blogger.comtag:blogger.com,1999:blog-5638372.post-82682630314505698452007-11-06T12:52:00.000-06:002007-11-06T12:52:00.000-06:00It should not compile because constructor with one...It should not compile because constructor with one int as argument is missing. Probably it would make sense to call existing constructor and give also species as parameter.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5638372.post-26221352114526597232007-10-31T15:54:00.000-05:002007-10-31T15:54:00.000-05:00For methods like this, I prefer the prefix "with" ...For methods like this, I prefer the prefix "with" instead of "set". After all, these methods are *not* setters in the way most Java programmers would expect.Nat Prycehttps://www.blogger.com/profile/14593335423887511402noreply@blogger.com