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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:15:36+00:00 2026-06-06T15:15:36+00:00

I have encountered a problem in one of my Java projects, which causes bugs.

  • 0

I have encountered a problem in one of my Java projects, which causes bugs.
The problem sounds as following:

I have two arrays. Let’s name them firstArray and secondArray. Object in this case is a seperate class created by me. It works, the array can be filled with objects of that type.

Object[] firstArray= new Object[];
Object[] secondArray = new Object[];

Now, when I get an element out of the first array, edit it and then copy it in the second array, the object from the first array gets altered too.

tempObj = firstArray[3];
tempObj.modifySomething();
secondArray[3] = tempObj;

Whenever I do this, the (in this case) 3rd element(actually 4th) of the first array gets the modifications. I don’t want this. I want the first Array to remain intact, unmodified, and the objects I have extracted from the first array and then modified should be stored in the second so that the second array is actually the first array after some code has been run.

P.S. Even if I get the element from the first array with Array.get(Array, index) and then modify it, the element still gets modified in the first array.

Hopefully you understood what I wanted to say, and if so, please lend me a hand 🙂

Thank you!

  • 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-06T15:15:38+00:00Added an answer on June 6, 2026 at 3:15 pm

    You’re going to have to create a new object.

    The problem is the modifySomething call. When you do that, it alters the object on which it’s called. So if you’ve only got one object (even by two names), you can’t call modifySomething or they will both change.

    When you say secondArray[3] = firstArray[3], you aren’t creating a new object: you’re just assigning a reference. Going through an intermediate temporary reference doesn’t change that.

    You’ll need code that looks like this:

    Object tempObj = firstArray[3].clone();
    tempObj.modifySomething();
    secondArray[3] = tempObj;
    

    The clone() method must return a new object divorced from the original but having identical properties.

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

Sidebar

Related Questions

I have encountered a problem in my application. I have two forms, one that
I have encountered a problem since one hour..take me as a beginner please.. The
I have encountered the following problem: 1) I have a Library ([x] Is Library
I have encountered a problem twice now whereby a producer thread produces N work
I have encountered a problem of placing data into the Table View. Here is
I have encountered the problem to start different version of jboss in the same
I have encountered this problem today and I don't have an explanation for it.
I am writing a class. I have encountered the problem in the title. Here
I'm currently exploring powershell capabilities, but I have encountered a problem that I have
I have encountered the strange problem, that linux c++ compiler includes the files from

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.