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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:03:37+00:00 2026-06-05T05:03:37+00:00

I created the following test to see how Java handles objects and it’s confusing

  • 0

I created the following test to see how Java handles objects and it’s confusing me quite a bit.

public class MyClass
{
    public String text = "original";
    public MyClass(String text)
    {
        this.text = text;
    }
}

Then i created the following 2 scenarios:

1.

String object1 = new String("original");
String object2 = new String("original");
object2 = object1;
object2 = "changed";
System.out.println(object1);
System.out.println(object2);

Result:

original
changed

2.

MyClass object1 = new MyClass("object1");
MyClass object2 = new MyClass("object2");
object2 = object1;
object2.text = "changed";
System.out.println(object1.text);
System.out.println(object2.text);

Result:

changed
changed

Now why is the text field shared like a static field?

  • 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-05T05:03:38+00:00Added an answer on June 5, 2026 at 5:03 am

    Now why is the text field shared like a static field?

    Look at this line:

    object2 = object1;
    

    That’s setting the value of the object2 variable to be the same as the value of the object1 variable. Those variable values are both references to objects – they’re not objects themselves.

    So after that line, both variables have values referring to the same object. If you change the object via one variable, you’ll still see that change via a the other variable. To put it in real world terms: suppose you have two slips of paper, each with your home address on, and give them to two different people. The first one goes and paints your front door red, then the second goes to visit your house – they will still see a red front doo.

    It’s very important to separate three concepts:

    • Objects
    • Variables
    • References

    The value of a variable (or any other expression, actually) is always either a primitive value (int, char etc) or a reference. It’s never a whole object.

    Changing the value of one variable never changes the value of a different variable, so here:

    String object1 = new String("original");
    String object2 = new String("original");
    object2 = object1;
    object2 = "changed";
    

    … we’re changing the value of object2 once to have the same value as object1, and then to have a different value, referring to a String object with the text “changed”. That never changes the value of object1.

    Does that help? If not, please ask about very specific cases – it’s easiest to pick several situations apart in detail rather than generalizing.

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

Sidebar

Related Questions

I have created a vector of class objects. The following program crashes with Pointer
Okay, so I created a JavaScript file called test.js which contains the following code:
So I made the following test for a class Board that would to be
I am trying the MVC3 exception handling, and came with following test code: public
I am particularly confused by the following test case: public void TestMapping() { var
I have the following domain classes: Holiday : class Holiday { String justification User
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
I have following spec to test controller method: context #create do it should redirect
I have the following table: CREATE TABLE TEST(ID TINYINT NULL, COL1 CHAR(1)) INSERT INTO
I have a table defined by the following SQL: CREATE TABLE test ( id

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.