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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:45:09+00:00 2026-06-02T11:45:09+00:00

When overwriting an existing variable, someThing of type Thing below in a method, what

  • 0

When overwriting an existing variable, someThing of type Thing below in a method, what is the correct way to do this?

doSomething(someThing);

void doSomething(Thing thing){
    //...
    thing = new Thing(...); // receives warning in Eclipse
}

or

someThing = doSomething(someThing);

Thing doSomething(Thing thing){
    //...
    return new Thing(...);
}
  • 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-02T11:45:11+00:00Added an answer on June 2, 2026 at 11:45 am

    Your first sample doesn’t change the variable someThing in the caller. The assignment is only visible in the doSomething method.

    The second sample does change someThing in the caller.

    So if you want to change someThing in the caller, option 2 is viable, while option 1 is not.

    See Is Java "pass-by-reference" or "pass-by-value"? for why this works that way.

    Assuming the following code, and a Thing that has a print method and a String member.

    void foo() {
      Thing one = new Thing("hello");  // 1
      bar(one);
      one.print();                   // 5
    }
    
    void bar(Thing two) {            // 2
      two = new Thing("bye");        // 3
    }                                // 4
    

    The assignment at point 1 first creates a new Thing object:

                           Thing{data="hello"}
    

    Then stores a reference to that in one:

    one *----- refs --------v
                           Thing{data="hello"}
    

    When you enter bar at point 2, a new reference to the same object is created:

    one *----- refs --------v
                           Thing{data="hello"}
    two *----- refs --------^
    

    Then line 3 does the same thing as line 1, i.e. create a new Thing object:

    one *----- refs --------v
                           Thing{data="hello"}
    two *----- refs --------^
    
                           Thing{data="bye"}
    

    then stores a reference to that new object in two:

    one *----- refs --------v
                           Thing{data="hello"}
    
    two *----- refs --------v
                           Thing{data="bye"}
    

    Notice that only two is modified. The assignment changes what two refers to.

    When you return from bar, at line 4, two goes out of scope, the “bye” Thing no longer has anything referencing it (and will eventually be garbage collected).

    one *----- refs --------v
                           Thing{data="hello"}
    
                           Thing{data="bye"} // dead, will be collected
    

    So at point 5, as you can see, hello will be printed – nothing ever changed the object that one refers to.

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

Sidebar

Related Questions

Will the below method overwrite the output file (process.txt) ? private static void CompareOrig()
How do I use the Stream CopyTo method without overwriting the existing output file?
I am trying to find a way to stop accidental overwriting of files when
I'm working with the new UIDocument features in iOS 5.0. I have an existing
How to check if headTitle is already used? To avoid appending or overwriting existing
Was overwriting a TextView class, and as I didn't find many sources, I was
is possible overwriting a Doctrine model in Symfony? I'm trying no change a notnull
I want to write to a file without overwriting anything. It is a text
Is Subversion's 'Lazy Copy' still lazy when overwriting a previously deleted file? I store
I just read this: http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx I was under the impression that overwriting Object or

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.