I'm curious about the possibility of implementing class-level methods in Java that could be abstract or default, like factory methods. While instance methods can be abstract or default, it seems static methods can only be implemented. My goal here isn't to create a static factory method, but rather an abstract one that could be utilized like a factory method. I've encountered situations where the lack of this feature has forced me to compromise my solutions significantly, and I provided a link to share an example of my experience with sealed types and data-oriented programming. I'd like to explore whether there's potential for change in this aspect of Java.
5 Answers
An interface can have a default method, like throwing an UnsupportedOperationException, but it still won't fully meet your needs for abstract factory behavior. If you're looking for methods that enforce implementation at the subclass level without runtime checks, this might not be the best way. You could also look into using annotation processors or code generation to manage these expectations more effectively.
You're expressing a common frustration! You want a way for rules to propagate to subclasses in a way that each subclass must implement, ensuring consistency in your regex definitions. You might consider using recursive generics in an abstract class to create the factory method, though it does require creating instances, which isn't a perfect solution. The workaround often involves reflection, which can be messy.
Yeah, but isn't creating a dummy instance first a bit of a clunky approach? I'd rather have something cleaner.
It's important to understand that static methods exist outside of instances and are resolved at compile-time, which is why we don't see abstract static methods in Java. Abstract methods are designed for polymorphism—a concept that only applies to instances, not statics. So technically, there's no way to implement an abstract static factory method because static methods don't have an associated instance. You can use instances to achieve factory behavior, but each factory doesn't need to be a static one.
I think you may have misunderstood my original post. I wasn't asking for a static abstract factory method—just an abstract factory method without the static requirement. I need a way for subclasses to provide their own implementation at the class level, not through static methods.
Have you considered using ArchUnit for enforcing your design constraints? While it's not a direct solution, it might provide the structure you need to check that subclasses adhere to your design principles without needing to dive into complex annotations or reflection.
Your needs seem well understood. It sounds like type classes or Scala-style companion objects would solve your issue elegantly, but since Java lacks these features, you might look into annotations to enforce certain requirements. This gives you compile-time checks, which would solve your problem directly without messy runtime solutions.
Absolutely, annotations could work. But I need to grasp how to implement those, as I'm not well-versed in dynamic class creation.
So are you suggesting the static method can throw an exception, or are you implying there might be a library that could help with this? I'm just trying to find a solution that fails at compile time rather than run time.