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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:55:34+00:00 2026-05-25T21:55:34+00:00

So I was reading this post and response no. 2. In that example, after

  • 0

So I was reading this post and response no. 2. In that example, after calling that method, does the Dog value at address 42, name’s changes to Max?

Dog myDog;

Dog myDog = new Dog("Rover");
foo(myDog);

public void foo(Dog someDog) {
    someDog.setName("Max");     // AAA
    someDog = new Dog("Fifi");  // BBB
    someDog.setName("Rowlf");   // CCC
}
  • 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-25T21:55:34+00:00Added an answer on May 25, 2026 at 9:55 pm

    Java is pass by value – always, both for primitives and objects.

    In the case of objects, the thing that’s passed is the reference to the object that lives out on the heap. A method cannot change what that reference points to when it’s passed in.

    If that reference points to an object that has mutable data, the method can alter its state.

    From “The Java Programming Language Second Edition”, by Ken Arnold and James Gosling (ISBN 0-201-31006-6 ) (probably from page 40–don’t have the book handy right now):

    Some people will say incorrectly that objects in Java are “pass by
    reference.” The term pass by reference properly means that when an
    argument is passed to a function, the invoked function gets a
    reference to the original value, not a copy of its value. If the
    function modifies its parameter, the value in the calling code will be
    changed because the argument and parameter use the same slot in
    memory. […] There is exactly one ParameterPassing mode in Java–pass
    by value–and that helps keep things simple.

    So let’s look at your example (with some improvements):

    public class Dog {
    
        private String name;
    
        public static void main(String [] args) {
            Dog myDog = new Dog("Rover");
            System.out.println("before foo: " + myDog);
            foo(myDog);
            System.out.println("after  foo: " + myDog);
        }
    
        public static void foo(Dog someDog) {
            someDog.setName("Max");     // AAA
            someDog = new Dog("Fifi");  // BBB
            someDog.setName("Rowlf");   // CCC
        }
    
        public Dog(String n) { this.name = n; }
    
        public String getName() { return this.name; }
    
        public void setName(String n) { this.name = n; }
    
        public String toString() { return this.name; }
    }
    

    Here’s the output:

    before foo: Rover
    after  foo: Max
    
    Tool completed successfully
    

    You can’t change what the reference that’s passed to foo points to, so setting it to the reference with the name “Fifi” at line BBB, and subsequently changing the name of that object at line CCC, does nothing. That instance is eligible for garbage collection when foo exits.

    The incoming reference that points to “Rover” has a mutable data member: its name. Changing its value at line AAA is reflected in the reference that was passed in; hence the different output.

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

Sidebar

Related Questions

After reading this post i realized that i cannot pass an anonymous type as
After reading this post I kinda felt in the same position as the guy
I've tried to do this several times with no luck. After reading this post
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
After reading this post, and the suggestion to use Team Edition for Database Professionals
Reading this post has left me wondering; are nightly builds ever better for a
I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after
Well I was reading this post and then I came across a code which
I've recently heard about the CaptureStackBackTrace function by reading this post . I cannot
Reading this blog post about HttpOnly cookies made me start thinking, is it possible

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.