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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:41:55+00:00 2026-06-14T01:41:55+00:00

I have a test class with fields which contain test content. It looks like:

  • 0

I have a test class with fields which contain test content. It looks like:

class Test {
  public static String s1 = " ... long string ... ";
  public static String s2 = " ... long string ... ";
  public static String s3 = " ... long string ... ";
}

Now I would like to use these fields in a loop, so I wrote an additional array

  public static String[] a = {s1, s2, s3};

This structure works fine, but is a bit ugly because everytime I add or delete a field, I have do change the array, too.

The only solution to re-structure my code without the manually manipulating of the array is to write it all in the array at once:

class Test {
  public static String[] a = {" ... long string ... ", " ... long string ... ",
                              " ... long string ... "};
}

As you can see, this makes the code unreadable, especially when we have to deal with > 10 long strings.

What could be a better structure ?

  • 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-14T01:41:56+00:00Added an answer on June 14, 2026 at 1:41 am

    If you want to use them in a loop, it makes me think that they are homogeneous in some way. Also the variable names (s1, s2, s3) are meaningless as they stand. So go for array (or use Arrays.asList() idiom).

    If each string has a different meaning and is used separately, definitely have separate variables. If you want to have best of both worlds (separate variables and a list that is easily maintainable, consider this):

    class Test {
        public static final String s1 = str(" ... long string ... ");
        public static final String s2 = str(" ... long string ... ");
        public static final String s3 = str(" ... long string ... ");
    
        public static final List<String> strings = new ArrayList<>();
    
        public static String str(String s) {
            strings.add(s);
            return s;
        }
    }
    

    And last but not least, remember about final keyword.

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

Sidebar

Related Questions

I have created a custom dialog box which requires 3 fields public class Test{
say I have: class Test { public static int Hello = 5; } This
I have a JUnit test class in which I have several static final int
I have a test sample about coherence lock-unlock mechanism like this: public class Test
I have one class Human , which contains two fields, age(int) , and name(String)
I have a generic abstract base class from which I would like to derive
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
If I have: class Test { private Vector2 v; public Vector2 Velocity { get
I have a class Test which is in lib folder and I want to
If I have a class called Test :: class Test { static std::vector<int> staticVector;

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.