1

I fount a util class in Spring named NestedExceptionUtils, and it is declared as an abstract class, why abstract? To prevent instances of it? But another class is not declared as an abstract class! Such as 'BeanDefinitionReaderUtils'. when should I do this?

1
  • Depends on which version you are looking at. The latest Spring 5.1 both are abstract. Utility classes are generally abstract and have a private no-args constructor to prevent sub-classing.
    – M. Deinum
    Dec 24, 2018 at 16:10

1 Answer 1

0

NestedExceptionUtils contains a single public static method and has no derived classes (within Spring). This suggests that it's abstract to prevent instantiation.

Another and a slightly better way to create utility classes in Java is to declare a class final and define a private no-args constructor. This is better than abstract utility class because it also suppresses extending the utility class.


Spring wasn't written by a single person, this could explain why different utility classes are written in different ways.

2
  • 1.the two example class are written by the same person named 'Juergen Hoeller', there is no reason two classes have a different style. 2.even if they are written by different people, don't spring team has a common style limited? I'm confused! Dec 24, 2018 at 15:08
  • Well, the author would be the best person to ask for the reasoning, of course. But note that BeanDefinitionReaderUtils has become abstract as well in Spring version 5.1.2.RELEASE. Dec 24, 2018 at 16:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.