Java Object as Parameter

Objects, like primitive types, can be passed as parameters to methods in Java. When passing an object as a parameter to a method, a reference to the object is passed rather than a copy of the object itself. This means that any modifications made to the object within the method will have an impact on the original object.

The general form to demonstrate "object as parameter" in Java is as follows:

public class MyClass {
    // Fields or attributes
    private int attribute1;
    private String attribute2;
    private double attribute3;
    
    // Constructor
    public MyClass(int attribute1, String attribute2, double attribute3) {
        this.attribute1 = attribute1;
        this.attribute2 = attribute2;
        this.attribute3 = attribute3;
    }
    
    // Method with object as parameter
    public void myMethod(MyClass obj) {
        // block of code to define this method
    }
    
    // More methods
}

An object belonging to the "MyClass" class is used as a parameter in the myMethod() function. This enables the method to operate on the passed object and access its attributes and methods.

Java objects as parameters example

Consider the following program demonstrating the object as parameters in Java:

Java Code
public class MyClass {
    private int attribute1;
    private String attribute2;
    private double attribute3;
    
    // Constructor
    public MyClass(int attribute1, String attribute2, double attribute3) {
        this.attribute1 = attribute1;
        this.attribute2 = attribute2;
        this.attribute3 = attribute3;
    }
    
    // Method with object as parameter
    public void myMethod(MyClass obj) {
        System.out.println("Attribute 1: " + obj.attribute1);
        System.out.println("Attribute 2: " + obj.attribute2);
        System.out.println("Attribute 3: " + obj.attribute3);
    }
    
    public static void main(String[] args) {
        MyClass myObject1 = new MyClass(10, "Hello", 3.14);
        MyClass myObject2 = new MyClass(20, "World", 6.28);
        
        // Call the method with object as parameter
        myObject1.myMethod(myObject2);
    }
}
Output
Attribute 1: 20
Attribute 2: World
Attribute 3: 6.28

The class MyClass in this code has three attributes labeled attribute1, attribute2, and attribute3 and a constructor that accepts three parameters. The class also includes a method called myMethod, which accepts as a parameter an object of the same class MyClass. The attributes of the passed object are printed using System.out.println() statements within the method. In the main() method, two MyClass objects are constructed using the constructor, and then the myMethod() method of the first object is invoked with the second object as a parameter. This results in the output of the attributes of the second object to the console.

Before closing the discussion on "object as parameters" in Java, I wanted to include another example, which is given below, that might help you more with the topic.

Java Code
public class ObjectAsParameterExample {
    public static void main(String[] args) {
        MyClass myObject1 = new MyClass("Edwin", 32);
        MyClass myObject2 = new MyClass("William", 29);

        // Call the method that takes a MyClass object as a parameter
        printPersonInfo(myObject1);

        // Call the method that takes two MyClass objects as parameters
        printBothPersonsInfo(myObject1, myObject2);
    }

    // Method that takes a MyClass object as a parameter
    public static void printPersonInfo(MyClass person) {
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }

    // Method that takes two MyClass objects as parameters
    public static void printBothPersonsInfo(MyClass myObject1, MyClass myObject2) {
        System.out.println("\n---Person 1---");
        System.out.println("Name: " + myObject1.getName());
        System.out.println("Age: " + myObject1.getAge());

        System.out.println("\n---Person 2---");
        System.out.println("Name: " + myObject2.getName());
        System.out.println("Age: " + myObject2.getAge());
    }
}

class MyClass {
    private String name;
    private int age;

    public MyClass(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}
Output
Name: Edwin
Age: 32

---Person 1---
Name: Edwin
Age: 32

---Person 2---
Name: William
Age: 29

The printPersonInfo() and printBothPersonsInfo() methods of the ObjectAsParameterExample class accept MyClass objects as parameters. The printPersonInfo() method prints a single person's name and age, whereas the printBothPersonsInfo() method prints the names and ages of two people.

The constructor is used to create two MyClass objects in the main() method, and then the printPersonInfo() method is called to print the information of the first object. Finally, the printBothPersonsInfo() method is invoked with both objects as parameters to print both objects' information.

Java Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!