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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:23:48+00:00 2026-06-12T13:23:48+00:00

This example is taken from the Java programming book; 4th Edition. After stumbling upon

  • 0

This example is taken from the Java programming book; 4th Edition.

After stumbling upon this code example I am mystified at why it still prints out the new reference to the object, even though we declared it to be ‘null’. In theory, we’re altering the references to the object which is shared between the whole program, though we initialize the object to null in the commonName method. At the point of the flow of control at commonName, the field in the body constructor is initialized to “Sirius”; when we alter the references to the object (In Java you call-by-value) the field is changed to Dog Star. The last line of the method we set the whole object to null, once we print out the object the runtime should greet us with a null references.

The only way to get round this is by setting the commonName method as final. Can any Java guru explain why this happens, especially in any call-by-value language.

class PassRef
{
    public static void main (String[] args) {
        Body sirius = new Body ("Sirus", null);
        System.out.println ("before: " + sirius);
        commonName(sirius);
        System.out.println("after: " + sirius);
    }

    public static void commonName (Body bodyref) {
        bodyref.name = "Dog Star";
        bodyref = null;
    }
}

The output:

before: 0 (Sirius)
after:  0 (Dog Star)
  • 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-12T13:23:49+00:00Added an answer on June 12, 2026 at 1:23 pm

    You’re mystified only because you don’t understand what pass by value means.

    The common method cannot alter the reference that it’s passed. That’s why null is ignored and you print the new value.

    You can alter the state of the object that the passed reference points to, but you cannot change the value of the reference itself.

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

Sidebar

Related Questions

I have an Hello world applet,taken from book example. HelloWorldApplet.java import java.awt.*; import javax.swing.*;
This example is taken from w3schools . CREATE TABLE Persons ( P_Id int NOT
I have taken this example from net . But when i tried it is
The ListNode example , taken from Scalas homepage goes like this: case class ListNode[+T](h:
All examples are taken from the SICP Book: http://sicpinclojure.com/?q=sicp/1-3-3-procedures-general-methods This was motivated from the
This is a small snippet of code taken from some of the examples that
I'm using a JTreeTable taken from the java sun example.. I have the following
The following example is taken from: http://php.net/manual/en/function.curl-multi-close.php#example-3540 This example will create two cURL handles,
I'm trying to use the example taken from the http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html , in which I've
I'm following the example of JTreeTable filesystem2 taken from the sun site http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html#updating_the My

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.