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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:38:09+00:00 2026-05-24T10:38:09+00:00

In this piece of code i pass a name into a method that modifies

  • 0

In this piece of code i pass a name into a method that modifies the String literal name but not the object itself, when the code exits the method, the object (as identified by the hashcode) is the same, however not the name that is modified in the method.

How should i explain this?

public class ObjectContentsPassByReferenceApp {
    private static void modifyObject(Bus bus) {
        bus.setName("SBS Transit");
    }

    public static void main(String args[]) {
        Bus bus;

        bus = new Bus();
        bus.setName("Trans Island Bus");

        System.out.println("Bus initially set to (hashcode): " + bus);
        System.out.println("Bus name: " + bus.getName());

        modifyObject(bus);

        System.out.println("After calling modifyObject (hashcode): " + bus);
        System.out.println("Bus name: " + bus.getName());
    }
}

class Bus {
    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Run results:

Bus initially set to (hashcode): sg.java.concepts.passByReference.Bus@8d2ed0
Bus name: Trans Island Bus
After calling modifyObject (hashcode): sg.java.concepts.passByReference.Bus@8d2ed0
Bus name: SBS Transit
  • 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-24T10:38:11+00:00Added an answer on May 24, 2026 at 10:38 am

    You haven’t overridden hashCode – therefore it will use the implementation in java.lang.Object, which doesn’t vary over the course of an object’s lifetime. You haven’t overridden equals either… which means that a.equals(b) will only return true for Bus a and Bus b if a and b refer to the exact same object – rather than objects with equal name.

    Note that the names in your code suggest that Java uses pass by reference. It doesn’t – it always uses pass by value, but those values are always either primitives or references – never actual objects. The same is true for simple assignment etc.

    In your code, you’re creating a single Bus object. Think of it as a real life bus. It has a name painted on it, and a serial number embossed on it (the latter being the hash code). When you call the method, that’s telling the method how to get to the Bus object – it’s not creating a new Bus. The method comes and paints over the name with a new one, but that does nothing to the serial number, which is the same as it ever was.

    Also note that name is not a string literal – it’s a string variable. Its initial value comes from a string literal, but changing the value later does nothing to the original string object.

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

Sidebar

Related Questions

This piece of code used to work in MVC 1 but it does not
Ok, so this piece of code works fine in the Debug but not in
I've got this piece of code: using Posix; int fuseguifs_getattr(string path, Posix.Stat *stbuf) {
This piece of code doesn't work; it's logging in into website which is using
I found this piece of code works in that i can programmatically creates a
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
This piece of code is from Adobe docs : package { import flash.display.Loader; import
This piece of code is supposed to go through a list and preform some
This piece of code gives a syntax error at the colon of elif process.loop(i,
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are

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.