GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — Java Object as Parameter

  • Java Programming Basics
  • Java Tutorial
  • Java Environment Setup
  • Java Separators
  • Java Data Types
  • Java Variables
  • Java Variable Scope
  • Java Type Casting
  • Java Operators
  • Java Increment Decrement
  • Java Left Shift
  • Java Right Shift
  • Java Bitwise Operators
  • Java Ternary Operator
  • Java Control Statements
  • Java if-else statement
  • Java for Loop
  • Java while Loop
  • Java do-while Loop
  • Java switch Statement
  • Java break Statement
  • Java continue Statement
  • Java Popular Topics
  • Java Arrays
  • Java Multidimensional Array
  • Java Strings
  • Java Methods
  • Java Date and Time
  • Java Exception Handling
  • Java File Handling
  • Java OOP
  • Java Classes and Objects
  • Java Constructors
  • Java Constructor Overloading
  • Java Object as Parameter
  • Java Returning Objects
  • Java Encapsulation
  • Java Abstraction
  • Java Inheritance
  • Java Polymorphism
  • Java Packages
  • Java Import Statement
  • Java Multithreading
  • Java Suspend Resume Stop Thread
  • Java Programming Examples
  • Java Programming Examples

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 »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.