Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 784191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:40:24+00:00 2026-05-14T20:40:24+00:00

In Java, I’d like to be able to define marker interfaces, that forced implementations

  • 0

In Java, I’d like to be able to define marker interfaces, that forced implementations to provide static methods. For example, for simple text-serialization/deserialization I’d like to be able to define an interface that looked something like this:

public interface TextTransformable<T>{

  public static T fromText(String text);

  public String toText();

Since interfaces in Java can’t contain static methods though (as noted in a number of other posts/threads: here, here, and here this code doesn’t work.

What I’m looking for however is some reasonable paradigm to express the same intent, namely symmetric methods, one of which is static, and enforced by the compiler. Right now the best we can come up with is some kind of static factory object or generic factory, neither of which is really satisfactory.

Note: in our case our primary use-case is we have many, many “value-object” types – enums, or other objects that have a limited number of values, typically carry no state beyond their value, and which we parse/de-parse thousands of time a second, so actually do care about reusing instances (like Float, Integer, etc.) and its impact on memory consumption/g.c.

Any thoughts?

EDIT1: To clear up some confusion – we have many, different objects fitting this pattern -really we’re trying to come up with something elegant for callers with 2 semantics:

  • Interfaces as contracts – unity of access (e.g. TextTransformable as a capability)
  • Requiring implementation by subclasses/implementations (e.g. force them to implement their own transformation

In terms of our thoughts for Flyweight, Factories – they’re both options we’ve considered, really we’re trying to see if we can find something more elegant though than relying on JavaDoc saying “implement a Factory and delegate calls to it, or expose it at XXX location by convention”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:40:25+00:00Added an answer on May 14, 2026 at 8:40 pm

    This is really appropriate for the Flyweight. That is basically what you are trying to accomplish with the statics. In terms of how to serve the Flyweight object so that you don’t create thousands of them, here are some ideas.

    One is the factory, which you state you thought about and rejected, although you didn’t state why (so any other ideas may suffer from the same problem) so I won’t go into it.

    Another is to have the value type have a method which can serve its converter. Something like this:

     public class ValueType {
           public static final TextTransformable<ValueType> CONVERT = ....
     }
    

    And then use it like this:

     ValueType value = ValueType.CONVERT.fromText(text);
    
     String text = ValueType.CONVERT.toText(value);
    

    Now that doesn’t enforce that all ValueType’s provide their converters via the same mechanism, for that I think you need a factory of some kind.

    Edit: I guess I don’t know what you find inelegant about a factory, but I think you are focused on callers, so how does this feel to you:

      ValueType value = getTransformer(ValueType.class).fromText(text);
    

    The above can be done with a static import of the factory and a method that has a signature like so:

       public static <T> TextTransformable<T> getTransformer(Class<T> type) {
             ...
       }
    

    The code to find the right transformer isn’t necessarily the prettiest, but from the callers perspective everything is nicely presented.

    Edit 2: Thinking about this further, what I see is that you want to control object construction. You can’t really do that. In other words, in Java you can’t force an implementer to use or not use a factory to create their object. They can always expose a public constructor. I think your problem is that you aren’t happy with the mechanisms for enforcing construction. If that understanding is right, then the following pattern may be of use.

    You create an object with only private constructors which wraps your value type. The object may have a generic type parameter to know what value type it wraps. This object is instantiated with a static factory method which takes a factory interface to create the “real” value object. All framework code that uses the object only takes this object as a parameter. It does not accept the value type directly, and that object cannot be instantiated without a factory for the value type.

    The problem with this approach is that it is quite restricting. There is only one way to create objects (those supported by the factory interface) and there is limited ability to use the value objects, as the code processing these text elements has limited interaction only through this object.

    I guess they say there isn’t a software problem that can’t be solved via an extra layer of indirection, but this may be a bridge too far. At least its food for thought.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 399k
  • Answers 399k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's not necessarily an error. As pointed out, 001 is… May 15, 2026 at 3:53 am
  • Editorial Team
    Editorial Team added an answer As I haven't seen it at serverfault yet, and the… May 15, 2026 at 3:53 am
  • Editorial Team
    Editorial Team added an answer You can use the debug functionality right into dev studio… May 15, 2026 at 3:53 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.