Can we pass objects as an argument in Java?

Yes, you can pass objects as arguments in Java. Consider the following example: Here we have a class with name Employee


In the following Java example, we a have a class with two instance variables name and age and a parameterized constructor initializing these variables.

We have a method coypObject() which accepts an object of the current class and initializes the instance variables with the variables of this object and returns it.

In the main method we are instantiating the Student class and making a copy by passing it as an argument to the coypObject() method.

 Live Demo

import java.util.Scanner;
public class Student {
   private String name;
   private int age;
   public Student(){
   public Student(String name, int age){ = name;
      this.age = age;
   public Student copyObject(Student std){ =;
      this.age = std.age;
      return std;
   public void displayData(){
      System.out.println("Name : ";
      System.out.println("Age : "+this.age);
   public static void main(String[] args) {
      Scanner sc =new Scanner(;
      System.out.println("Enter your name ");
      String name =;
      System.out.println("Enter your age ");
      int age = sc.nextInt();
      Student std = new Student(name, age);
      System.out.println("Contents of the original object");
      System.out.println("Contents of the copied object");
      Student copyOfStd = new Student().copyObject(std);


Enter your name
Enter your age
Contents of the original object
Name : Krishna
Age : 20
Contents of the copied object
Name : Krishna
Age : 20