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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:07:53+00:00 2026-06-17T21:07:53+00:00

I have some doubts about a constructor. There is a class Foo that can

  • 0

I have some doubts about a constructor.

There is a class “Foo” that can be created in three different “status” we could call them “YOUNG”, “ADULT” and “OLD”.

I want to underline that they cannot be seen as different classes because the object will evolve and if it has been created as “YOUNG” it will become “ADULT” and then “OLD” and so on..

My question is: how can I define one or more constructors to diversify these three typologies??

I see some possibilities, but no one is an “elegant” solution..

1) Create a constructor with an int as input

public Foo(int i)
{
    switch (i)
    {
         case 0:
         .
         .
         .
         case 1:
         .
         .
         .
         case 2:
         .
         .
         .
    }
}

but i do not like it because it is not so clear to understand if another person see this code.

2) Create a blank constructor and then create three different methods like

public Foo()
{

}

public void setYoungFoo()
{
    .
    .
    .
}

public void setAdultFoo()
{
    .
    .
    .
}

public void setOldFoo()
{
    .
    .
    .
}

This could be a clear way to resolve the problem, but i would resolve this problem in the constructor..

3) Could static variables usefull in this context?

public static final String "YOUNG";
public static final String "ADULT";
public static final String "OLD";


public Foo(String field)
{

}

I do not know how fill this constructor because I have never used static final variables (I have seen them used in some Java classes like Calendar even if not used in the contructor).

Could you please comment these three option to underline what are their disadvantages because I am quite sure that they can not be a good solution..

  • 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-17T21:07:55+00:00Added an answer on June 17, 2026 at 9:07 pm

    Use method 1, but with an enum instead of an int. You can even inline the enum in the class, just make it public static.

    class Foo {
        public static enum fooState {
           young, adult, old;
        }
    
        public Foo(fooState i) {
            switch(i) {
            case young: ... break;
            case adult: ... break;
            case old: ... break;
            default: throw new InvalidStateException();
        }
        ...
    }
    

    Edit: You asked for opinions on your 3 options, here are mine:

    1. Option 1 technically does the same as my version, but uses magic numbers (i.e. numbers without assigned name), which makes it harder to understand (both for other people and for your future self)
    2. Option 2 leaves the class in an undefined or even invalid state between the Object creation and the call to set*Foo(). If you can make the transition to Young/Adult/Old at any time, it would be an option to set the Object to e.g. Young state in the constructor and let the user use the already necessary functions to switch to another state. Depends on the situation if this is a good option, but it is a valid one.
    3. Option 3 is basically the same as Option 1, just slower because it involves string comparison. The code is easier to read if you call “new Foo(Foo.YOUNG)”, but it is not intuitive that you have to write it that way. Also, it would be possible to write “new Foo(“Alligator”)”, which would in the best case create a runtime error.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some doubts about Newsstand/In-App Purchase that I really can not figure out.
I have some doubts about the categories of log4j. I have three categories ...
i have some doubts about the PL/SQL context, are there: the PL/SQL context is
I have some simple doubts about NSOperation and GCD that I have not found
I have some doubts about construction and initialization order guarantees in C++. For instance,
I'm a beginner about javascript, and I have some doubts about the variables of
I have programmed an emulator, but I have some doubts about how to organizate
I have some doubts. What is querystrng? how we can use it? what are
I have some doubts about how to concatenate MvcHtmlString instances because of this information
I have an idea of organising a game loop. I have some doubts about

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.