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

The Archive Base Latest Questions

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

I’m trying to override Variables that are already defined. Here is my code: package

  • 0

I’m trying to override Variables that are already defined.

Here is my code:

package com.diesal11;

import java.lang.reflect.Array;

public class Test{

private class List {
    public String[] words;

    public List(String[] array) {
        this.words = array;
    }
}

public List[] all;

public Test() {
    this.all = new List[2];
    String[] array = new String[2];

    array[0] = "One";
    array[1] = "Two";
    this.all[0] = new List(array);

    array[0] = "Three";
    array[1] = "Four";
    this.all[1] = new List(array);

    System.out.println(this.all[0].words[0]);
    System.out.println(this.all[0].words[1]);
    System.out.println(this.all[1].words[0]);
    System.out.println(this.all[1].words[1]);
}

public static void main(String[] args) {
    Test test = new Test();
}

}

The problem is the console prints out:

Three
Four
Three
Four

How can I fix this? the actual code I need this for is setup in this way so it can’t change much.

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-05-23T21:04:16+00:00Added an answer on May 23, 2026 at 9:04 pm

    The problem is you are storing a reference to the array passed to the List constructor.
    You then change that same array and pass it to the 2nd List object.

    Instead, create a new array and pass that in like this:

    ...
    String[] array = new String[2];
    
    array[0] = "One";
    array[1] = "Two";
    this.all[0] = new List(array);
    
    array = new String[2]; // CREATE A NEW ARRAY
    array[0] = "Three";
    array[1] = "Four";
    this.all[1] = new List(array);
    ...
    

    EDITED – Added style-related feedback

    Your bigger problem is this code has lots of style issues:

    • Don’t call you class List: You should avoid using class names from the JDK, especially from the Collections framework
    • Make your MyList class static: It doesn’t need to access any fields from the containing class Test – it’s a DTO
    • From a design point of view, your code has highlighted the problem with keeping references to mutable objects – you have no control over what the calling code does to your object (in this case, as array).

    A simple change that avoids this problem would be this:

    static MyList {
        String[] words;
    
        public MyList(String... words) {
            this.words = words;
        }
    }
    ...
    this.all[0] = new List("one", "two");
    

    The syntax String... words is called a “varargs” parameter – it creates an array on the fly that only the method has a reference to (although arrays can also be passed in, giving you the same problem).
    The only safe way is to make a copy of the array and store that, or provide a method that allows you to add a word (using a List to hold the words for example)

    • In general, try to avoid arrays – prefer using Collections
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.