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

Information can be passed to methods as parameter. Parameters act as variables inside the method.

Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

The following example has a method that takes a String called fname as parameter. When the method is called, we pass along a first name, which is used inside the method to print the full name:

public class Main {   static void myMethod(String fname) {     System.out.println(fname + " Refsnes");   }   public static void main(String[] args) {     myMethod("Liam");     myMethod("Jenny");     myMethod("Anja");   } } // Liam Refsnes // Jenny Refsnes // Anja Refsnes

Try it Yourself »

When a parameter is passed to the method, it is called an argument. So, from the example above: fname is a parameter, while Liam, Jenny and Anja are arguments.

Multiple Parameters

You can have as many parameters as you like:

public class Main { static void myMethod(String fname, int age) { System.out.println(fname + " is " + age); } public static void main(String[] args) { myMethod("Liam", 5); myMethod("Jenny", 8); myMethod("Anja", 31); } } // Liam is 5 // Jenny is 8 // Anja is 31

Try it Yourself »

Note that when you are working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

Return Values

The void keyword, used in the examples above, indicates that the method should not return a value. If you want the method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method:

public class Main {   static int myMethod(int x) {     return 5 + x;   }   public static void main(String[] args) {     System.out.println(myMethod(3));   } } // Outputs 8 (5 + 3)

Try it Yourself »

This example returns the sum of a method's two parameters:

public class Main {   static int myMethod(int x, int y) {     return x + y;   }   public static void main(String[] args) {     System.out.println(myMethod(5, 3));   } } // Outputs 8 (5 + 3)

Try it Yourself »

You can also store the result in a variable (recommended, as it is easier to read and maintain):

public class Main {   static int myMethod(int x, int y) {     return x + y;   }   public static void main(String[] args) {     int z = myMethod(5, 3);     System.out.println(z);   } } // Outputs 8 (5 + 3)

Try it Yourself »

A Method with If...Else

It is common to use if...else statements inside methods:

public class Main {   // Create a checkAge() method with an integer variable called age   static void checkAge(int age) {     // If age is less than 18, print "access denied"     if (age < 18) {       System.out.println("Access denied - You are not old enough!");     // If age is greater than, or equal to, 18, print "access granted"     } else {       System.out.println("Access granted - You are old enough!");     }   }   public static void main(String[] args) {     checkAge(20); // Call the checkAge method and pass along an age of 20   } } // Outputs "Access granted - You are old enough!"

Try it Yourself »



  • Read
  • Discuss
  • Improve Article

    Save Article

    Although Java is strictly passed by value, the precise effect differs between whether a primitive type or a reference type is passed. When we pass a primitive type to a method, it is passed by value. But when we pass an object to a method, the situation changes dramatically, because objects are passed by what is effectively call-by-reference. Java does this interesting thing that’s sort of a hybrid between pass-by-value and pass-by-reference. 
    Basically, a parameter cannot be changed by the function, but the function can ask the parameter to change itself via calling some method within it.

    • While creating a variable of a class type, we only create a reference to an object. Thus, when we pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument.
    • This effectively means that objects act as if they are passed to methods by use of call-by-reference.
    • Changes to the object inside the method do reflect the object used as an argument.

    Illustration: Let us suppose three objects ‘ob1’ , ‘ob2’ and ‘ob3’ are created: 

    ObjectPassDemo ob1 = new ObjectPassDemo(100, 22); ObjectPassDemo ob2 = new ObjectPassDemo(100, 22); ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1);

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

    From the method side, a reference of type Foo with a name a is declared and it’s initially assigned to null. 

    boolean equalTo(ObjectPassDemo o);

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

    As we call the method equalTo, the reference ‘o’ will be assigned to the object which is passed as an argument, i.e. ‘o’ will refer to ‘ob2’ as the following statement execute. 

    System.out.println("ob1 == ob2: " + ob1.equalTo(ob2));

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

    Now as we can see, equalTo method is called on ‘ob1’ , and ‘o’ is referring to ‘ob2’. Since values of ‘a’ and ‘b’ are same for both the references, so if(condition) is true, so boolean true will be return. 

    if(o.a == a && o.b == b)

    Again ‘o’ will reassign to ‘ob3’ as the following statement execute. 

    System.out.println("ob1 == ob3: " + ob1.equalTo(ob3));

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

    • Now as we can see, the equalTo method is called on ‘ob1’ , and ‘o’ is referring to ‘ob3’. Since values of ‘a’ and ‘b’ are not the same for both the references, so if(condition) is false, so else block will execute, and false will be returned.

    In Java we can pass objects to methods as one can perceive from the below program as follows:

    Example:

    class ObjectPassDemo {

        int a, b;

        ObjectPassDemo(int i, int j)

        {

            a = i;

            b = j;

        }

        boolean equalTo(ObjectPassDemo o)

        {

            return (o.a == a && o.b == b);

        }

    }

    public class GFG {

        public static void main(String args[])

        {

            ObjectPassDemo ob1 = new ObjectPassDemo(100, 22);

            ObjectPassDemo ob2 = new ObjectPassDemo(100, 22);

            ObjectPassDemo ob3 = new ObjectPassDemo(-1, -1);

            System.out.println("ob1 == ob2: "

                               + ob1.equalTo(ob2));

            System.out.println("ob1 == ob3: "

                               + ob1.equalTo(ob3));

        }

    }

    Outputob1 == ob2: true ob1 == ob3: false

    Defining a constructor that takes an object of its class as a parameter

    One of the most common uses of object parameters involves constructors. Frequently, in practice, there is a need to construct a new object so that it is initially the same as some existing object. To do this, either we can use Object.clone() method or define a constructor that takes an object of its class as a parameter.

    Example

    class Box {

        double width, height, depth;

        Box(Box ob)

        {

            width = ob.width;

            height = ob.height;

            depth = ob.depth;

        }

        Box(double w, double h, double d)

        {

            width = w;

            height = h;

            depth = d;

        }

        double volume() { return width * height * depth; }

    }

    public class GFG {

        public static void main(String args[])

        {

            Box mybox = new Box(10, 20, 15);

            Box myclone = new Box(mybox);

            double vol;

            vol = mybox.volume();

            System.out.println("Volume of mybox is " + vol);

            vol = myclone.volume();

            System.out.println("Volume of myclone is " + vol);

        }

    }

    OutputVolume of mybox is 3000.0 Volume of myclone is 3000.0

    Returning Objects

    In java, a method can return any type of data, including objects. For example, in the following program, the incrByTen( ) method returns an object in which the value of an (an integer variable) is ten greater than it is in the invoking object.

    Example

    class ObjectReturnDemo {

        int a;

        ObjectReturnDemo(int i) { a = i; }

        ObjectReturnDemo incrByTen()

        {

            ObjectReturnDemo temp

                = new ObjectReturnDemo(a + 10);

            return temp;

        }

    }

    public class GFG {

        public static void main(String args[])

        {

            ObjectReturnDemo ob1 = new ObjectReturnDemo(2);

            ObjectReturnDemo ob2;

            ob2 = ob1.incrByTen();

            System.out.println("ob1.a: " + ob1.a);

            System.out.println("ob2.a: " + ob2.a);

        }

    }

    Note: When an object reference is passed to a method, the reference itself is passed by use of call-by-value. However, since the value being passed refers to an object, the copy of that value will still refer to the same object that its corresponding argument does. That’s why we said that java is strictly pass-by-value.

    This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.