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 6155147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:23:04+00:00 2026-05-23T20:23:04+00:00

I’m implementing a standard as an object oriented library in Java. Standard includes many

  • 0

I’m implementing a standard as an object oriented library in Java. Standard includes many messages which passing over network through terminals. Every message is implemented as a single class.

Some of the fields is represented as char types and those fields have one of the values that is defined in the standard. For example,

public class OutgoingMessage {
    private char action;

and action has these values,

'0' - Do not print
'1' - Print
'2' - Print and Forward
'3' - Print and Reply
'F' - Forward
'R' - Reply

Also some of the classes have more than 2 fields like this. Defining those as constants in the class can be messy in these situations.

So I’m trying to implement those values as

public class OutgoingMessage {
    private char action;

    public final class ActionTypes {
        public static final char DO_NOT_PRINT = '0';
        public static final char PRINT = '1';
        ...

And using as below

...
message.setAction(OutgoingMessage.ActionTypes.DO_NOT_PRINT);
...
message.setFooBar(OutgoingMessage.FooBarTypes.FOO_BAR);
...

What do you think? Is there anything wrong with this approach? How would you define these constants in the library?

Thanks a lot

  • 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-23T20:23:04+00:00Added an answer on May 23, 2026 at 8:23 pm

    Use enums in preference to int or char constants:

    public enum Action {
        DoNotPrint,
        Print,
        PrintAndForward,
        PrintAndReply,
        Forward,
        Reply
    }
    
    public class OutgoingMessage {
         private Action action;
    

    If you need to associate a char with the action, do this:

    public enum Action {
        DoNotPrint('0'),
        Print('1'),
        PrintAndForward('2'),
        PrintAndReply('3'),
        Forward('F'),
        Reply('R');
    
        private static Map<Character, Action> map = new HashMap<Character, Action>() {{
            for (Action action : Action.values()) {
                put(action.getChar(), action);
            }
        }};
    
        private final char c;
    
        private Action(char c) {
            this.c = c;
        }
    
        public char getChar() {
            return c;
        }
    
        public static Action parse(char c) {
            if (!MapHolder.map.containsKey(c))
                throw new IllegalArgumentException("Invalid char: " + c);
            return MapHolder.map.get(c);
        }
    }
    

    Here’s how you can use the parse method:

    public static void main(String[] args) {
        System.out.println(Action.parse('2')); // "PrintAndForward"
        System.out.println(Action.parse('x')); // throws IllegalArgumentException
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.