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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:37:50+00:00 2026-06-04T20:37:50+00:00

Following program output In second v.i:15 In first v.i:20 why its not 15 in

  • 0

Following program output

In second v.i:15
In first v.i:20

why its not 15 in both cases.Object of Value is passed and then object reference is changed in the Second method.Second method its 15 as it should be and seems like in First method it should also be 15

public class Test {
    /**
     * @param args
     */
    class Value{
        public int i = 15;
    }
    public static void main(String[] args) {
        Test t = new Test();
        t.first();
    }
    public void first(){
        Value v = new Value();
        v.i = 25;
        second(v);
        System.out.println("In First v.i:" + v.i);
    }
    public void second(Value v){
        v.i = 20;
        Value val = new Value();
        v = val;
        System.out.println("In second v.i:" + v.i);
    }
}
  • 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-04T20:37:54+00:00Added an answer on June 4, 2026 at 8:37 pm

    Java method implementations are call by[reference to, in case of objects,]value but not exactly a call by reference.

    You are passing an object Value v means an in-line, method scope variable v is referring to the object v created in method first(). That means any modifications to the same object, referred by v, will also reflect at calling end. But, in your second method, you are creating a fresh object for Value but pointing to the method scope variable v. This new objects memory location is not the same as that of passed in method parameter. To identify the difference, check the hashCode of the objects created using their reference variables.

    And hence changing the instance variables of v in method second will not be returned to the caller of the method, unless the method is returning the altered object. Your method returns a void here.

    Programmers, most of the time, get confused with the same reference names used in caller and called methods.

    Look at the following example to understand the difference. I have included a third' and afourth` methods, to explain it further.

    public class Test {
        class Value {
            int i = 15;
        }
    
        public void second( Value v ) {
            System.out.println( " 2.1.1: entered: v.i = " + v.i ); // 25
            System.out.println( " 2.1.2: v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
    
            v = new Value();
            v.i = 9;
            System.out.println( " 2.2.1:   new V: v.i = " + v.i ); // 9
            System.out.println( " 2.2.2: v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
        } // second(v)
    
        public Value third( Value v ) {
            System.out.println( " 3.1.1:  entered: v.i = " + v.i ); // 25
            System.out.println( " 3.1.2:  v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
    
            v = new Value();
            v.i = 9;
            System.out.println( " 3.2.1:  created: v.i = " + v.i ); // 9
            System.out.println( " 3.2.2:  v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
    
            return v;
        } // third(v)
    
        public Value fourth( final Value v ) {
            System.out.println( " 4.1.1:entered: v.i = " + v.i ); // 9
            System.out.println( " 4.1.2:v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
    
            /**********************************
            // The final local v can't be assigned. It must be blank and not using a compound assignment.
            // meaning, you are not allowed to change its memory location,
            // but can alter its content, if permitted
            // v = new Value();
            //**********************************/
    
            v.i = 45;
            System.out.println( " 4.2.1:changed: v.i = " + v.i ); // 45
            System.out.println( " 4.2.2:v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
    
            return v;
        } // fourth(v)
    
        public void first() {
            System.out.println( "1.1.1: entered: ..." );
    
            Value v = new Value();
            System.out.println( "1.2.1: created; v.i = " + v.i ); // 15
            v.i = 25;
            System.out.println( "1.2.2: changed: v.i = " + v.i ); // 25
            System.out.println();
    
            System.out.println( "1.3.1: before calling second(v) ..." );
            System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
            second( v );
            System.out.println( "1.3.2: returning from second(v) ..." );
            System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
            System.out.println();
    
            System.out.println( "1.4.1:  before calling third(v) ..." );
            System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
            v = third( v );
            System.out.println( "1.4.2:  returning from third(v) ..." );
            System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
            System.out.println();
    
            System.out.println( "1.5.1: before calling fourth(v) ..." );
            System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
            v = fourth( v );
            System.out.println( "1.5.2: returning from fourth(v) ..." );
            System.out.println( " v.i = " + v.i + ", v.hashCode() = " + v.hashCode() + "; v = " + v.toString() );
        } // first()
    
        public static void main( String ... a ) {
            Test _this = new Test();
            _this.first();
        } // psvm(...)
    } // class Test
    

    When you run the above example, you may see an output as below:

    1.1.1: entered: ...
    1.2.1: created; v.i = 15
    1.2.2: changed: v.i = 25
    
    1.3.1: before calling second(v) ...
     v.i = 25, v.hashCode() = 1671711; v = Test$Value@19821f
     2.1.1: entered: v.i = 25
     2.1.2: v.hashCode() = 1671711; v = Test$Value@19821f
     2.2.1:   new V: v.i = 9
     2.2.2: v.hashCode() = 11394033; v = Test$Value@addbf1
    1.3.2: returning from second(v) ...
     v.i = 25, v.hashCode() = 1671711; v = Test$Value@19821f
    
    1.4.1:  before calling third(v) ...
     v.i = 25, v.hashCode() = 1671711; v = Test$Value@19821f
     3.1.1:  entered: v.i = 25
     3.1.2:  v.hashCode() = 1671711; v = Test$Value@19821f
     3.2.1:  created: v.i = 9
     3.2.2:  v.hashCode() = 4384790; v = Test$Value@42e816
    1.4.2:  returning from third(v) ...
     v.i = 9, v.hashCode() = 4384790; v = Test$Value@42e816
    
    1.5.1: before calling fourth(v) ...
     v.i = 9, v.hashCode() = 4384790; v = Test$Value@42e816
     4.1.1:entered: v.i = 9
     4.1.2:v.hashCode() = 4384790; v = Test$Value@42e816
     4.2.1:changed: v.i = 45
     4.2.2:v.hashCode() = 4384790; v = Test$Value@42e816
    1.5.2: returning from fourth(v) ...
     v.i = 45, v.hashCode() = 4384790; v = Test$Value@42e816
    

    If you really want to hold the changes made to an object instanceVariableV in a called method, say fifth(), the other possibility is to declare v as instance variable.

    Following example will explain the differences.

    public class Test {
        Value instanceVariableV = null; // v
    
        // rest of other variables and methods here
            // ...
    
        public void fifth() {
            System.out.println( " 5.1.1:entered: instanceVariableV = " + instanceVariableV ); // null
            // null, hence no hashCode(), and no toString() will work
    
            // let us create an instance of Value
            instanceVariableV = new Value();
            System.out.println( " 5.2.1:created: instanceVariableV = " + instanceVariableV ); // Test$Value@9304b1
            System.out.println( " 5.2.2: instanceVariableV.i = " + instanceVariableV.i ); // 15
            System.out.println( " 5.2.3: hashCode = " + instanceVariableV.hashCode() ); // 9634993
    
            instanceVariableV.i = 20;
            System.out.println( " 5.3.1:changed: instanceVariableV.i = " + instanceVariableV.i ); // 20
            System.out.println( " 5.3.2: hashCode = " + instanceVariableV.hashCode() ); // 9634993 // not changed 
        } // fifth()
    
        public void first() {
            // continuation of code
    
            System.out.println( "1.6.1: before calling fifth() ..." );
            System.out.println( " instanceVariableV = " + instanceVariableV  );
            fifth();
            System.out.println( "1.6.2: returning from fifth() ..." );
            System.out.println( " instanceVariableV = " + instanceVariableV  );
            if ( instanceVariableV != null ) {
                // must be different from the one when created new
                System.out.println( " .i = " + instanceVariableV.i );
                // this won't differ
                System.out.println( " .hashCode() = " + instanceVariableV.hashCode() );
            }
        } // first()
    
        public static void main( String ... a ) {
            // ...
            System.out.println( "\r\nmain(...): vInstanceVariable = " + _this.instanceVariableV );
            if ( _this.instanceVariableV != null ) {
                // must be different from the one when created new
                System.out.println( " .i = " + _this.instanceVariableV.i );
                // this won't differ
                System.out.println( " .hashCode() = " + _this.instanceVariableV.hashCode() );
            }
        } // psvm(...)
    

    When you run with the above extended example, you may see an output as below:

    1.6.1: before calling fifth() ...
     instanceVariableV = null
     5.1.1:entered: instanceVariableV = null
     5.2.1:created: instanceVariableV = Test$Value@9304b1
     5.2.2: instanceVariableV.i = 15
     5.2.3: hashCode = 9634993
     5.3.1:changed: instanceVariableV.i = 20
     5.3.2: hashCode = 9634993
    1.6.2: returning from fifth() ...
     instanceVariableV = Test$Value@9304b1
     .i = 20, .hashCode() = 9634993
    
    main(...): vInstanceVariable = Test$Value@9304b1
     .i = 20
     .hashCode() = 9634993
    

    Hope this helps you.

    Other references:

    1. Does Java pass by reference or pass by value?
    2. Is java pass by reference? (A posting on SO)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is the output of the following program just int3 and not int3&4 ?
I do not understand the output of the following program: #include <iostream> #define FOO
I have written the following C program. The output is 32. Why is this?
The output of the below code is as following: not equal equal Note the
Consider following program: static void Main (string[] args) { int i; uint ui; i
In following program . I have one doubt. I have declared one global variable
The following program is a basic linked list divided in 3 classes. In the
The following program doesn't build in VS11 beta, gcc 4.5, or clang 3.1 #include
I have the following program to open lot's of sockets, and hold them open
I have the following program to browse all virtual directories and their sub directories

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.