Object as an Argument in Java

Object as an argument is use to establish communication between two or more objects of same class as well as different class, i.e, user can easily process data of two same or different objects within function.
By Abhishek Jain Last updated : January 14, 2024

In Java, When a primitive type is passed to a method ,it is done by use of call-by-value . Objects are implicitly passed by use of call-by-reference.

This means when we pass primitive data types to method it will pass only values to function parameters so any change made in parameter will not affect the value of actual parameters.

Whereas Objects in java are reference variables, so for objects a value which is the reference to the object is passed. Hence the whole object is not passed but its referenced gets passed. All modification to the object in the method would modify the object in the Heap.

Passing Object as Parameter in Function

class Add {
  int a;
  int b;

  Add(int x, int y) // parametrized constructor 
  {
    a = x;
    b = y;
  }
  void sum(Add A1) // object  'A1' passed as parameter in function 'sum'
  {
    int sum1 = A1.a + A1.b;
    System.out.println("Sum of a and b :" + sum1);
  }
}

public class Main {
  public static void main(String arg[]) {
    Add A = new Add(5, 8);
    /* Calls  the parametrized constructor 
    with set of parameters*/
    A.sum(A);
  }
}

Output

Sum of a and b :13

While creating a variable of class type, we only create a reference to an object.

When we pass this reference to a function, the parameters that receive it will refer to the same object as that referred to by the argument.


Passing Object as Parameter in Constructor

One of the most common uses of objects as parameters involves constructors. A constructor creates a new object initially the same as passed object. It is also used to initialize private members.

class Add {
  private int a, b;

  Add(Add A) {
    a = A.a;
    b = A.b;
  }

  Add(int x, int y) {
    a = x;
    b = y;
  }

  void sum() {
    int sum1 = a + b;
    System.out.println("Sum of a and b :" + sum1);
  }
}

public class Main {
  public static void main(String arg[]) {
    Add A = new Add(15, 8);
    Add A1 = new Add(A);
    A1.sum();
  }
}

Output

Sum of a and b :23

Returning the Object from Function

In java, a function can return any type of data, including class type objects.

For ex: In the program given below, the add() function return an object which contain sum of values of two different Numbers(objects).

import java.util.Scanner;

class TwoNum {
  private int a, b;
  Scanner kb = new Scanner(System.in);

  void getValues() // getValues() take values of a,b for every no.
  {
    System.out.print("Enter a: ");
    a = kb.nextInt();
    System.out.print("Enter b: ");
    b = kb.nextInt();
  }

  void putValues() // putValues() show values for every no.
  {
    System.out.println(a + " " + b);
  }

  TwoNum add(TwoNum B) /*class type function add() takeobject 'B' as parameter*/ {
    TwoNum D = new TwoNum(); //object D act as instance variable
    D.a = a + B.a;
    D.b = b + B.b;
    return (D); //returning object D
  }
}

public class Main {
  public static void main(String arg[]) {
    TwoNum A = new TwoNum();
    A.getValues();
    A.putValues();

    TwoNum B = new TwoNum();
    B.getValues();
    B.putValues();

    TwoNum C;
    /*object A calls add() passing object B
    as parameter and result are return at C*/
    C = A.add(B);

    C.putValues();
  }
}

Output

Enter a: 23
Enter b: 34
23 34
Enter a: 25
Enter b: 35
25 35
48 69   /*sum*/


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.