> I am not a big fan of extending Static Utility classes. And Joshua Bloch is not either from his book "Effective Java"...
Yep I'm sure he will never like this lol. Melloware <melloware...@gmail.com> 于2020年9月6日周日 下午10:18写道: > I am not a big fan of extending Static Utility classes. And Joshua > Bloch is not either from his book "Effective Java"... > > See: https://www.informit.com/articles/article.aspx?p=1216151&seqNum=4 > > > On 9/6/2020 10:06 AM, Xeno Amess wrote: > > class AUtil { > > public static String getStringA() { > > return "A"; > > } > > } > > > > class ExtendedAUtil extends AUtil { > > public static String getStringABCDE() { > > return "ABCDE"; > > } > > } > > > > public class Main { > > public static void main(String[] args) { > > System.out.println(ExtendedAUtil.getStringA()); > > System.out.println(ExtendedAUtil.getStringABCDE()); > > } > > } > > > > Maybe it's better to add some runnable codes :) > > > > Xeno Amess <xenoam...@gmail.com> 于2020年9月6日周日 下午10:05写道: > > > >> Well sometimes we want to extend(or modify) some behaviors of one Util > >> Class.That is why I don't want the constructor be private. > >> For example, there be a AUtil: > >> > >> public class AUtil{ > >> public static String getStringA(){ > >> return "A"; > >> } > >> } > >> > >> then some people need a function that returns "ABCDE". > >> Actually "ABCDE" is useless for any other repos, so we can never pass > the > >> pr to put the getABCDE function to AUtil. > >> And that people also need the getStringA function. > >> So there be a way: > >> > >> public class ExtendedAUtil extends{ > >> public static String getStringABCDE(){ > >> return "ABCDE"; > >> } > >> } > >> > >> In this way that people can use ExtendedAUtil as an extended AUtil. > >> I admit that might be sugar and tricky, but it will help shorten the > codes. > >> BUT if we make the AUtil have only private constructor, then we cannot > do > >> such things, as class who have private constructors only cannot be > extended. > >> > >> Melloware <melloware...@gmail.com> 于2020年9月6日周日 下午9:55写道: > >> > >>> That is why I love Lombok's @UtilityClass. > >>> > >>> https://projectlombok.org/features/experimental/UtilityClass > >>> > >>> It enforces a static class is truly static by making the constructor > >>> private and throwing an exception, making sure all methods are static, > >>> marking the class as Final etc. > >>> > >>> > >>> On 9/6/2020 9:53 AM, Xeno Amess wrote: > >>>>> Inheritance in Java on the static side is > >>>> not the same as on the instance side > >>>> > >>>> Yep, I know it. It will not override but just, hiding. > >>>> I admit it might confuse people sometimes. > >>>> > >>>>> subclassing a class that only > >>>> provides static methods is no help. > >>>> > >>>> Well actually I personally use it for a shortcut or something. > >>>> Of course we can do this by fork/wrap the static functions one by one, > >>> but > >>>> extending it directly can make the codes shorter. > >>>> > >>>> Gary Gregory <garydgreg...@gmail.com> 于2020年9月6日周日 下午9:48写道: > >>>> > >>>>> On Sun, Sep 6, 2020 at 9:44 AM Xeno Amess <xenoam...@gmail.com> > wrote: > >>>>> > >>>>>> The idea behind not making *Util constructors private is that it > makes > >>>>>> people be able to extend that class. > >>>>>> for example: > >>>>>> > >>>>>> > >>> > https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/StringUtils.java#L9627 > >>>>> > >>>>> I have not see a use case that requires instances of classes that > only > >>>>> provide static methods in a long time, like the Javadoc mentions, > there > >>>>> used to be JavaBean tools that needed this, and some UI builders > IIRC, > >>> but > >>>>> I do not see a case of it today. Inheritance in Java on the static > >>> side is > >>>>> not the same as on the instance side, so subclassing a class that > only > >>>>> provides static methods is no help. > >>>>> > >>>>> Gary > >>>>> > >>>>> > >>>>>> Gary Gregory <garydgreg...@gmail.com> 于2020年9月6日周日 下午9:39写道: > >>>>>> > >>>>>>> The idea behind making *Util constructors private is that it does > not > >>>>>> make > >>>>>>> sense to instantiate a class that only has static methods. > >>>>>>> > >>>>>>> Gary > >>>>>>> > >>>>>>> > >>>>>>> On Sun, Sep 6, 2020 at 12:49 AM Xeno Amess <xenoam...@gmail.com> > >>>>> wrote: > >>>>>>>> for example: can we make its constructor public instead of > private? > >>>>>>>> > >>> --------------------------------------------------------------------- > >>> To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > >>> For additional commands, e-mail: dev-h...@commons.apache.org > >>> > >>> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org > For additional commands, e-mail: dev-h...@commons.apache.org > >