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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:07:16+00:00 2026-06-10T02:07:16+00:00

I learned that when you modify a variable in Java it doesn’t change the

  • 0

I learned that when you modify a variable in Java it doesn’t change the variable it was based on

int a = new Integer(5);
int b = a;
b = b + b;
System.out.println(a); // 5 as expected
System.out.println(b); // 10 as expected

I assumed a similar thing for objects. Consider this class.

public class SomeObject {
    public String text;

    public SomeObject(String text) {
        this.setText(text);
    }

    public String getText() {
        return text;
    }   

    public void setText(String text) {
        this.text = text;
    }
}

After I tried this code I got confused.

SomeObject s1 = new SomeObject("first");
SomeObject s2 = s1;
s2.setText("second");
System.out.println(s1.getText()); // second as UNexpected
System.out.println(s2.getText()); // second as expected

Please explain to me why changing any of the objects affects the other one. I understand that the value of variable text is stored in the same place in memory for both of the objects.

Why the values for variables are independent but correlated for objects?

Also, how to duplicate SomeObject, if simple assignment does not do the job?

  • 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-10T02:07:18+00:00Added an answer on June 10, 2026 at 2:07 am

    Every variable in Java is a reference. So when you do

    SomeClass s2 = s1;
    

    you just point s2 to the same object as s1 points to. You are actually assigning the value of the reference s1 (which points to an instance of SomeClass) to s2. If you modify s1, s2 will be modified as well (because it points to the same object).

    There is an exception, primitive types: int, double, float, boolean, char, byte, short, long. They are stored by value. So when using =, you only assign the value, but they can not point to the same object (because they are not references). This means that

    int b = a;
    

    only sets the value of b to the value of a. If you change a, b will not change.

    At the end of the day, everything is assignment by value, it’s just the value of the reference and not the value of the object (with the exception of primitive types as mentioned above).

    So in your case, if you want to make a copy of s1, you can do it like this:

    SomeClass s1 = new SomeClass("first");
    SomeClass s2 = new SomeClass(s1.getText());
    

    Alternatively, you can add a copy constructor to SomeClass that takes an instance as argument and copies it into its own instance.

    class SomeClass {
      private String text;
      // all your fields and methods go here
    
      public SomeClass(SomeClass copyInstance) {
        this.text = new String(copyInstance.text);
      }
    }
    

    With this you can copy an object pretty easily:

    SomeClass s2 = new SomeClass(s1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I learned that Servlet reference implementation is included in the Java EE SDK. And
I learned that copy something to kill buffer, I can use the kill-new buffer
I learned that when executing commands in Python, I should use subprocess. What I'm
I learned that repaint() wasn't an actual method that repaints the screen. Rather, it
I learned that Xcode can compile c code, that means can we write whole
I recently learned that table valued functions are not allowed with Entity Framework 4.1.
I've learned that MySQL can compress communication between servers and clients. Compression is used
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I googled around and learned that Lift encourages view-first development, lazy loading of entities,
I have learned that Windows uses UTF-16LE on x86/x64 systems. What about Linux? Which

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.