tag:blogger.com,1999:blog-7290642316743338665.post8335299354058147085..comments2023-09-20T05:23:44.886-05:00Comments on behind the times: Java Private Constructor Cleverness?Hamlet D'Arcyhttp://www.blogger.com/profile/04008870357169725586noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-7290642316743338665.post-83624294344673082732010-02-22T21:22:44.018-06:002010-02-22T21:22:44.018-06:00This comment has been removed by a blog administrator.木須炒餅Jerryhttps://www.blogger.com/profile/11813785579307536291noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-70299190485103255712009-02-16T19:36:00.000-06:002009-02-16T19:36:00.000-06:00Hi i like the way its presented in the form of dia...Hi <BR/><BR/>i like the way its presented in the form of dialog/discussion ....very intresting ....and it drives the reader till the end ......its nice .....good job !!!Unknownhttps://www.blogger.com/profile/03326625789649148715noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-45869689906275734612008-07-19T12:57:00.000-05:002008-07-19T12:57:00.000-05:00There are issues with private constructors (and pr...There are issues with private constructors (and private members in general) that make code reuse difficult, if not impossible. Recently I've been working on an implementation of a wrapper around org.w3c.dom.* and javax.parsers.DocumentBuilder* as host objects for Rhino. We had a need to extend the parser we were using (xerces) to annotate DOM nodes with line numbers, and so I subclassed Xerces DOMParser and extended it as needed (they have an example of doing this actually), but then when I went to put together a custom DocumentBuilder class i learned that I couldn't actually subclass Xerce's DocumentBuilderImpl because the member that held the dom parser was private, and accessed by member name, and so the only solution was to break out of doing it the JAXP way and call DOMParser directly, or copying all the code from DocumentBuilderImpl to change 1 or 2 lines. I opted for the first, which makes it a lot harder for people to swap out the DOM parser with another one.alexblhttps://www.blogger.com/profile/07910313501358762052noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-49034221048159786142008-06-25T23:18:00.000-05:002008-06-25T23:18:00.000-05:00It reminds me most of Stephen Colbert's "Formidabl...It reminds me most of Stephen Colbert's "Formidable Opponent" segments.<BR/><BR/>Anyway, there is a lot of places when you want -- nay, <B>need</B> -- a constructor, even for a utility class. Spring is a classic example: if I want to specify one of your utility class methods via Spring configuration, I'm going to need to be able to instantiate the class to create it as a bean. Also, if I'm not keen on using static imports, it's easy to do a <I>private static final StringUtils su = new StringUtils()</I>: the extra space burnt for the object won't be noticed.<BR/><BR/>This is one of the cases where efforts to prevent "bad behavior" are really overstepping their bounds.Robert Fischerhttps://www.blogger.com/profile/15576124960718643532noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-85094990886971143832008-06-25T15:42:00.000-05:002008-06-25T15:42:00.000-05:00Coincidentally, I just posted about the same topic...Coincidentally, I just <A HREF="http://www.javablackbelt.com/blog/2008/06/use-extension-to-avoid-utility-class.html" REL="nofollow">posted</A> about the same topic, giving an example of replacing a utility class by a subclass. <BR/><BR/>Is it super-important? Probably not. I see it as a subset of "Keep data and behaviour together". And it looks nicer.Moandji Ezanahttps://www.blogger.com/profile/01507510837237294496noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-4050380460309334872008-06-25T09:00:00.000-05:002008-06-25T09:00:00.000-05:00Yes, it is a blatant Little Lisper rip-off. I thin...Yes, it is a blatant Little Lisper rip-off. <BR/><BR/>I think both voices have good points, and are equally valid on their own. I think we're drawn towards wanting to synthesize two opinions into a "correct" middle-way, and assuming that either extreme end of the spectrum is necessarily wrong. But in this case there is no need for a middle way. There are a lot of people who will see the "throw Assert..." idiom and dogmatically apply it everywhere. And that's fine with me (so long as they use an IDE template). And there are some people who don't bother putting private constructors in, and I'm OK with that too. The only extreme I would reject is that there is no place for utility classes within Java. <BR/><BR/>Perhaps this is just a variation of the whitespace debate, in which there is no correct answer yet people are will to argue ad nauseum about it. :)Hamlet D'Arcyhttps://www.blogger.com/profile/04008870357169725586noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-45103746317789896072008-06-25T08:01:00.000-05:002008-06-25T08:01:00.000-05:00Hofstadter is nice, of course. But what this remi...Hofstadter is nice, of course. But what this reminds me of most is The Little Lisper. :)Brian Oxleyhttps://www.blogger.com/profile/06617364377560752378noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-82068085434366212692008-06-25T06:34:00.000-05:002008-06-25T06:34:00.000-05:00"...util classes should be final..." A private con..."...util classes should be final..." A private constructor already prevents a class from being extended.Rubén Barrosohttps://www.blogger.com/profile/12083467867497017753noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-11202218513278522862008-06-25T04:40:00.000-05:002008-06-25T04:40:00.000-05:00you forgot that util classes should be final(you c...you forgot that util classes should be final(you cant mock final classes). the real problem here is that people dont write tests for util classes.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-60138080436740562552008-06-25T04:14:00.000-05:002008-06-25T04:14:00.000-05:00"....you can't swap out the utility class during t..."....you can't swap out the utility class during testing in order to isolate your class under test..."<BR/><BR/>Is this really an issue though? If we are truly talking about a utility class (DateUtils, StingUtils etc.) then surely there is no state involved, each and every method is idempotent and orthogonal to one another.<BR/><BR/>And really, what is the alternative? The fact is that java.lang.String is not equipped with every conceivable method.<BR/><BR/>Singletons and factory methods exhibit problems though but if you use the service provider pattern these are actually not too hard to swap out through class path dependency injection.<BR/><BR/>"...even if a programmer inside your class decides that it should be done..."<BR/><BR/>You can always boil things down to a HR issue. No language construct will shield you from this - even if I do feel we are in need of some better abstractions in Java, particular when dealing with resources.Casper Banghttps://www.blogger.com/profile/09493174484116672294noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-37958171403458255982008-06-25T03:51:00.000-05:002008-06-25T03:51:00.000-05:00What is the smallest/simplest static method that n...What is the smallest/simplest static method that needs to be abstracted into an interface so that multiple implementations are possible? Math.abs (remember there is also StrictMath.abs)? The identity function? I'll use [] instead of chevrons for generics:<BR/><BR/>public static [T] T identity(T t) {<BR/> return t; }<BR/><BR/>I'd say you only need to do that indirection when you actually need to use multiple implementations.Ricky Clarksonhttps://www.blogger.com/profile/13845104548520132930noreply@blogger.comtag:blogger.com,1999:blog-7290642316743338665.post-70820071529274365212008-06-25T01:50:00.000-05:002008-06-25T01:50:00.000-05:00I dig the style. Makes me want to go read Hofstad...I dig the style. Makes me want to go read Hofstadter again. :) I was waiting for the Tortoise to show up. <BR/><BR/>I also like a practical approach that takes the attitude that your co-workers aren't idiots. (Unless of course they are, in which case you're screwed anyway.)Alex Millerhttps://www.blogger.com/profile/01929662536395624733noreply@blogger.com