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

  • SEARCH
  • Home
  • 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 7830335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:11:17+00:00 2026-06-02T11:11:17+00:00

I have created an enum and I’m trying to allow my enum to support

  • 0

I have created an enum and I’m trying to allow my enum to support a String.format operation that gets unlimited number of parameters are return a string.
I only managed to return an object and after using this method I have to do a toString()/casting. I am guessing there’s a more “clean” way to do it, or maybe to override better the toString() method. Basically, I wanted to support the toString() method but sadly didn’t manage to do that so I created this method. As you can see it’s named text(..) and not toString().

How can I do this better? The ideal solution I wanted was something like toString(..) which returns a String.

public enum MY_ENUM {

    VALUE_A("aaa %s"), VALUE_B("bbb %s");

    private String text;

    MY_ENUM(String text) {
        this.text = text;
    }

    public String text() {
        return this.text;
    }

    public Object text(final Object... o) {
        return new Object() {
            @Override
            public String toString() {
                return String.format(text(), o);
            }
        };
    }
}
  • 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-06-02T11:11:19+00:00Added an answer on June 2, 2026 at 11:11 am

    I see where you’re going… I think this is what you want (tested, and it works):

    public String toString(Object... o) {
        return String.format(text, o);
    }
    

    For a design point of view, I would not publish the text (ie have the getter) unless you really need to – the fact that text is used as a format string is an implementation choice. I would simply do this:

    public static enum MY_ENUM {
    
        VALUE_A("aaa %s bbb %s"),
        VALUE_B("bbb %s");
    
        private final String text;
    
        MY_ENUM(String text) {
            this.text = text;
        }
    
        public String toString(Object... o) {
            return String.format(text, o);
        }
    }
    

    As an aside, I really like the idea of the class. Haven’t seen it before.

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

Sidebar

Related Questions

I have created a StringToMapConverter for converting string in predefined format to a Map
I have created an enum as so: enum Types { hi, hello, bye }
I have a web service in which I created an enum.. i have a
I have created a mac application using the WebView. But issue is that webView
I have created a axis2 webservice including a method with multiple parameters: class Service
I have created a custom data type enum like so: create type bnfunctionstype as
I have created an attribute that accepts a (params) array in its constructor. internal
I have created 1 enum when I use it as a property for a
I have created an XSD and have run XSD.exe on top of that .xsd
Well, I have recently created a programs in C# that alters a program's memory,

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.