Passing Variables to Methods and Constructors Tutorial

When we pass a variable as an argument to a method or constructor, we are utilizing a pass-by-value technique. If we invoke a method or a constructor that contains a parameter list in its signature, we must pass a corresponding list of arguments that match that signature. The argument list can consist of literals, primitive variables, or reference variables. It is important to understand that the values of the actual argument variables initialize newly created parameter variables. I'm going to say it again ... it is important to understand that the values of the actual argument variables initialize newly created parameter variables. With regards to passing reference variables into parameters, you may hear someone say that Java uses pass-by-reference, but that is incorrect. Java only supports pass-by-value.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md PassVariables
C:\Java>cd PassVariables
C:\Java\PassVariables>Notepad PassVariables.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class PassVariables {
    public static void main(String args[]) {
        int argInt = 99;
        Box argBox = new Box();
        argBox.length = 5;
        argBox.height = 5;
        argBox.width = 5;

        System.out.println("\nOriginal values from main method...");
        System.out.println("argInt = " + argInt);        
        System.out.println("argBox dimensions: " + argBox.length + ", " + argBox.height + ", " + argBox.width);   
        System.out.println("argBox = " + argBox);

        Acme proof = new Acme();
        proof.passByValue(41, argInt, argBox);

        System.out.println("\n-----------\nValues back in main method....");
        System.out.println("argInt = " + argInt);
        System.out.println("argBox dimensions: " + argBox.length + ", " + argBox.height + ", " + argBox.width);
        System.out.println("argBox = " + argBox);   
    }
}

class Acme {

    void passByValue(int paramLiteral, int paramInt, Box paramBox) {
        System.out.println("\n-----------\nInvoking passByValue....");  
      
        paramLiteral++; // it was a literal as an argument - nothing to prove!
        System.out.println("Nothig to prove, paramLiteral = " + paramLiteral);

        paramInt++;
        System.out.println("paramInt = " + paramInt);

        // Box anotherBox = new Box(); // ???
        // paramBox = anotherBox; // ???

        paramBox.length = 14;
        paramBox.height = 14;
        paramBox.width = 14;
        System.out.println("paramBox dimensions: " + paramBox.length + ", " + paramBox.height + ", " + paramBox.width); 
        System.out.println("paramBox = " + paramBox);  
    }
    
}

class Box {
    int length = 0;
    int height = 0;
    int width = 0;
} 



Now switch back to the command prompt (CMD) and type in javac PassVariables.java and press Enter.
Now type in java PassVariables and press Enter.
Uncomment the two commented lines, recompile and run again.


C:\Java\PassVariables>javac PassVariables.java
C:\Java\PassVariables>java PassVariables
Original values from main method...
argInt = 99
argBox dimensions: 5, 5, 5
argBox = Box@659e0bfd

----------
Invoking passByValue...
Nothing to prove, paramLiteral = 42
paramInt = 100
paramBox dimensions: 14, 14, 14
paramBox = Box@2a139a55

----------
Values back in main method...
argInt = 99
argBox dimensions: 5, 5, 5
argBox = Box@659e0bfd


Final thoughts

It this tutorial I demonstrated the principle of pass-by-value using just methods, the exact same principals apply to constructors as well.
Don't just take my word for it, James Gosling (if you are studying Java, you should know who he is) once stated:

"Some people will say incorrectly that objects are passed 'by reference.'
In programming language design, 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…. The Java programming
language does not pass objects by reference; it passes object references by value.
Because two copies of the same reference refer to the same actual object,
changes made through one reference variable are visible through the other.
There is exactly one parameter passing mode — pass by value — and that helps keep things simple."

— James Gosling, et al., The Java Programming Language, 4th Edition


Tutorials