When an object is passed as an argument to a method what actually passed is?

   main() {
      int i = 10, j = 20;
      swapThemByVal(i, j);
      cout << i << "  " << j << endl;     // displays 10  20
      swapThemByRef(i, j);
      cout << i << "  " << j << endl;     // displays 20  10
      ...
   }

   void swapThemByVal(int num1, int num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
   }

   void swapThemByRef(int& num1, int& num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
   }

- [Instructor] Passing variables into methods by value can sometimes however be confusing. And that's because when you call a method and you pass in an object as the parameter rather than a primitive data type like we see here then the variable that contains the reference to the object is what is passed. To state this more fully, when an object is passed into a method as a parameter, a copy of the variable on the stack containing the reference to the object is passed. And it's this variable on the stack that is passed by value. It means that a copy of the pointer to the object is created, the object itself is not copied and in fact it's not the object that's passed into a method but rather a pointer to the object. For example, if we pass a copy of a list into a method, then the parameter value will be a copy of the myList variable which is a pointer to the object on the heap. So I hope that's clear, many programmers misinterpret this as objects are passed by reference. But they're not, and let's look at an example of why this is significant. Here's some code which creates a customer and then calls a method which changes the customer's name and prints out the customer's name at the end. Assume that the customer class has been well written and it has get and set methods as you'd expect. What would the outputs of this be? I suggest you pause the video and work through this example using the stack and heap pictures that I've been showing you to work out what would happen. When you've had a think about it press resume on the video and I'll talk it through. So let's talk through this example. First of all, we have a line that creates the new customer object. The constructor of the customer takes a string so the first thing that will happen is a string will be created on the heap, then the customer will be created with a property that references the string object and a variable will be created on the stack pointing to this customer object. When we call rename customer because we passed the value of the customer by value, a new variable will be created on the stack called cust which will be given a copy of the pointer to the customer on the heap. Now we change the name of the customer. I hope you remember that strings are immutable in Java, that is you can't change the value of a string if you do this you actually create a new string object. So what happens when this line runs is that a new string object is created on the heap and Java changes the pointer from the name in the customer object to this new string. The original string object is no longer referenced from anywhere and so it can be garbage collected at some point in the future. Finally, we exit the rename customer method and go to the print line. And now our C variable references the amended object customer which has a name pointing to the new string Diane so the output should be the name Diane. I hope you were able to predict this outcome, if you were then well done as this means that you now do understand the mechanism of how variables are passed between methods.

Practice while you learn with exercise files

Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

Download courses and learn on the go

Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

When you write your method, you determine the number and type of the arguments required by that method. You declare the type and name for each argument in the method signature. For example, the following is a method that computes the monthly payments for a home loan based on the amount of the loan, the interest rate, the length of the loan (the number of periods), and the future value of the loan (presumably the future value of the loan is zero because at the end of the loan, you've paid it off).

double computePayment(double loanAmt, double rate, double futureValue, int numPeriods) {
    double I, partial1, denominator, answer;

    I = rate / 100.0;
    partial1 = Math.pow((1 + I), (0.0 - numPeriods));
    denominator = (1 - partial1) / I;
    answer = ((-1 * loanAmt) / denominator) - ((futureValue * partial1) / denominator);
    return answer;
}

double loanAmt

double rate

double futureValue

int numPeriods

type name

As you can see from the body of the method, you simply use the argument name to refer to its value.

Argument Types

In Java, you can pass an argument of any valid Java data type into a method. This includes simple data types such as doubles, floats and integers as you saw in the

Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
1 method above, and complex data types such as objects and arrays. Here's an example of a method that accepts an array as an argument. In this example, the method creates a new Polygon object from a list of
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
2,
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
3 points.
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
Unlike some other languages, you cannot pass methods into Java methods.

Argument Names

When you declare an argument to a Java method, you provide a name for that argument. This name is used within the method body to refer to the item. As with other names in Java, an argument name must be a legal Java identifier.

A method argument can have the same name as one of the class's member variables. If this is the case, then the argument is said to hide the member variable. Arguments that hide member variables are often used in constructors to initialize a class. For example, take the following Circle class and its constructor:

class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
The Circle class has three member variables
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
2, y and
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
5. In addition, the constructor for the Circle class accepts three arguments each of which shares its name with the member variable for which the argument provides an initial value.

The argument names hide the member variables. Thus using

Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
2,
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
3 or
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
5 within the body of the constructor refers to the argument, not to the member variable. To access the member variable, you must reference it through
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
9--the current object:

class Circle { 
    int x, y, radius; 
    public Circle(int x, int y, int radius) {  
        this.x = x;
        this.y = y;
        this.radius = radius;
    } 
}
Names of method arguments cannot be the same as another argument name for the same method, the name of any variable local to the method, or the name of any parameter to a
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
0 clause within the same method.

Pass by Value

In Java methods, arguments of simple data types are passed by value. When invoked, the method receives the value of the variable passed in, not a reference to the variable itself. For example, consider this series of Java statements which attempts to retrieve the current color of a Pen object in a graphics application:
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
At the time when the
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method is called, the variables
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
2,
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
3, and
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
4 all have the value -1. The caller is expecting the
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method to pass back the red, green and blue values of the current color in the
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
2,
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
3, and
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
4 variables.

However, the Java runtime passes the variables' values (

class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
9) into the
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method; not a reference to the
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
2,
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
3, and
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
4 variables. Thus the call to
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 actually looks like this:
class Circle { 
    int x, y, radius; 
    public Circle(int x, int y, int radius) {  
        this.x = x;
        this.y = y;
        this.radius = radius;
    } 
}
5.

When control passes into the

class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method, new, local variables are created with the names of the parameters provided in the method signature and are initialized to the value passed into the method.

class Pen {
    int redValue, greenValue, blueValue;
    void getRGBColor(int red, int green, int blue) {
        . . .
    }
}
So,
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 gets access to the values of
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
2,
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
3, and
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
4 in the caller through its local variables
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
1,
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
2, and
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
3, respectively. The method gets a new copy of the values to use locally. Any changes made to those local variables are not reflected in the original variables from the caller.

Now, let's look at the implementation of

class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 within the Pen class that the method signature above implies:

class Pen {
    int redValue, greenValue, blueValue;
    void getRGBColor(int red, int green, int blue) {
	red = redValue;
	green = greenValue;
	blue = blueValue;
    }
}
This method will not work. When control gets to the
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
5 statement in this code snippet,
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1's local variables
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
1,
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
2, and
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
3 no longer exist. Therefore the assignments made to those variables had no effect;
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
2,
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
3, and
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
4 are all still equal to
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
9.
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
Passing variables by value affords the programmer some safety. Methods cannot unintentially modify a variable that is outside of its scope. However, you often want a method to be able to modify one or more of its arguments. The
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method is a case in point. The caller wants the method to return three values through its arguments. However, the method cannot modify its arguments, and, furthermore, a method can only return one value through its return value. So, how can a method return more than one value, or have an effect (modify some value) outside of its scope.

To allow a method to modify a argument, you must pass in an object. Objects in Java are also passed by value, however, the value of an object is a reference. So, the effect is that the object is passed in by reference. When passing an argument by reference, the method gets a reference to the object. A reference to an object is the address of the object in memory. Now, the local variable within the method is referring to the same memory location as the variable within the caller.

So, let's rewrite the

class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method so that it actually works. First, you must introduce a new object, RGBColor, that can hold the red, green and blue values of a color in RGB space.

class RGBColor {
    public int red, green, blue;
}
Now, we can rewrite
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 so that it accepts an RGBColor object as an argument. The
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method returns the current color of the pen by setting the
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
1,
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
2 and
. . .
int r = -1, g = -1, b = -1;
pen.getRGBColor(r, g, b);
System.out.println("red = " + r + ", green = " + g + ", blue = " + b);
. . .
3 member variables of its RGBColor argument.
class Pen {
    int redValue, greenValue, blueValue;
    void getRGBColor(RGBColor aColor) {
	aColor.red = redValue;
	aColor.green = greenValue;
	aColor.blue = blueValue;
    }
}
And finally, let's rewrite the calling sequence:
Polygon polygonFrom(Point listOfPoints[]) {
    . . .
}
0
The modifications made to the RGBColor object within the
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method affect the object created in the calling sequence, because the names
class Pen {
    int redValue, greenValue, blueValue;
    void getRGBColor(int red, int green, int blue) {
	red = redValue;
	green = greenValue;
	blue = blueValue;
    }
}
2 (in the calling sequence) and
class Pen {
    int redValue, greenValue, blueValue;
    void getRGBColor(int red, int green, int blue) {
	red = redValue;
	green = greenValue;
	blue = blueValue;
    }
}
3 (in the
class Circle {
    int x, y, radius;
    public Circle(int x, int y, int radius) {
        . . .
    }
}
1 method) refer to the same object.

The data type wrapper classes (Float, Integer, and so on) provided in the java.lang package are particularly useful for returning a single value whose type is one of Java's built-in simple types through a method's arguments.

When an object is passed as an argument to a method _______ is actually passed?

When an object is passed as an argument to a method, this is actually passed. this is the name of a reference variable that is always available to an instance method and refers to the object that is calling the method. This array field holds the number of elements that the array has.

What happens when an object is passed as an argument?

If your remote method has an argument that's an object type, the argument is passed as a full copy of the object itself! For remote calls, Java passes objects by object copy, not reference copy.

When an object is passed as an argument to a method what is passed into the methods parameter variable?

When code in a method changes the value of a parameter, it also changes the value of the argument that was passed into the parameter. When an object, such as a String , is passed as an argument, it is actually a reference to the object that is passed. The contents of a String object cannot be changed.

When an object is passed as an argument to a method the objects address?

Instance methods should be declared static. Instance methods do not have the key word static in their headers. When an object is passed as an argument to a method, the object's address is passed into the method's parameter variable.