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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:29:45+00:00 2026-06-15T21:29:45+00:00

I’m very to Java and I use enum for the first time like this:

  • 0

I’m very to Java and I use enum for the first time like this:

    public class Web {
        public String baseUrl;
        public static enum Environment {

            DEVELOPMENT("http://development") ,
            PRODUCTION("http://production"),
            SANDBOX("http://sandbox");

            public final String baseUrl;

            private Environment(String baseUrl)
            {
                this.baseUrl = baseUrl;
            }
        }
    }

The enum Environment has the three constants DEVELOPMENT, PRODUCTION,SANDBOX. The Web class also has the baseUrl to which the Environment‘s baseUrl to be set ( not sure this is the good practice to do so ).

For setting up the baseUrl I’m currently doing like this :

new Web().setBaseUrl(Web.Environment.PRODUCTION.baseUrl)

I’m not sure this is the right way to use the enums with the classes. Are there any way to directly set Web baseUrl to enums baseUrl.

Am I missing something here.

Thanks in advance

  • 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-15T21:29:46+00:00Added an answer on June 15, 2026 at 9:29 pm

    I think you’re on the right track, but you’re losing some of the strength of Java’s enum by accessing the URL in this manner. Instead, use the type of the enum to help ensure that you pass correct values to your method. That is, pass the enum alone, and let the method extract whatever value from it. For example:

    new Web().setEnvironment(Web.Environment.PRODUCTION);
    

    Now you can only pass Environments to your Web class, rather than any ol’ string.

    [Edit] Then your Web.setEnvironment method looks like this:

    public void setEnvironment(Environment environment) {
        this.baseUrl = environment.getBaseUrl();
    }
    

    This way, I can’t come along and call new Web().setEnvironment("marvo") by accident. It enforces a certain level of correctness.

    And vishal_aim is right. Even with Enums you should practice data hiding and encapsulation, so make the instance variable private, and provide an accessor like getBaseUrl() to retrieve the value.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like to count the length of a string with PHP. The string
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.