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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:27:26+00:00 2026-05-13T17:27:26+00:00

Is it possible to set a variable, if i want to have it flexible?

  • 0

Is it possible to set a variable, if i want to have it flexible? I think an exmaple makes it easier to understand.

String hallo1;
String hallo2;

for(int i = 0; i < 2; i++) {
 hallo & i = Integer.toString(i);
}
  • 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-13T17:27:26+00:00Added an answer on May 13, 2026 at 5:27 pm

    If you really have a need to have variables with names not known at compile time, you can achieve this effect by creating a structure that holds arbitrary names and values. Like, you could create a HashMap where the key is the name. Then your example above becomes something like:

      HashMap myData=new HashMap();
      for (int i=0;i<2;++i)
      {
        myData.put("hallo"+i,Integer.toString(i));
      }
    

    Later you’d pull them out with:

      String whatever=myData.get("hallo1");
    

    etc.

    Of course you can’t access values from the map as variables directly, you’d always have to do put and get to update and retrieve them, but the concept is the same.

    That said, I’d be very cautious about doing this, because it makes the code difficult to maintain. If the names that you need are really coming out of the blue — if you’re writing generic code to read an arbitrary database table whose name was typed in by the user at runtime or something like that — cool. But if you’re thinking that this is a handy shortcut for something like:

      if (region.equals("1"))
        region1Total+=amount;
      else
        region2Total+=amount;
    

    My simple answer would be DON’T!! The code is much easier to maintain if you use the IF statement and normal variables. Then anyone reading the code can look at your declarations and see what all the possible variables are. You can do text searches to find everywhere they’re used. If you mis-spell a variable name, instead of magically creating a new variable you will get a clean compile-time error message. Etc.

    On the other hand, if you COULD write something like

      String n=getInputFromScreen();
      String s=getAnotherInputFromScreen();
      hallo & n = s;
    

    you would have no idea what variables exist in your program and no way to track where they are used.

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

Sidebar

Related Questions

I'm trying to understand if it's possible to create a set of variables that
Is it possible to set a default selection on an NSPopupButton? I have one
I have an unbalanced quarterly panel data set with missing values. I want to
I'm working on android project and I want to set some variable on running
I have a variable obj: Option[MyObject] and want to extract multiple variables from it
Possible Duplicate: AJAX request fails to process $_SESSION variable I have some strange issue
I have a variable and I have set it to 3. every time when
Possible Duplicate: How to access a variable in a PHP class that is set
I want to have a variable that I can access anywhere by importing a
We have 2 tables with identical structure and based on a variable I want

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.