- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Example
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.
import java.util.Scanner; public class Student { private String name; private int age; public Student(){ } public Student(String name, int age){ this.name = name; this.age = age; } public Student copyObject(Student std){ this.name = std.name; this.age = std.age; return std; } public void displayData(){ System.out.println("Name : "+this.name); System.out.println("Age : "+this.age); } public static void main(String[] args) { Scanner sc =new Scanner(System.in); System.out.println("Enter your name "); String name = sc.next(); System.out.println("Enter your age "); int age = sc.nextInt(); Student std = new Student(name, age); System.out.println("Contents of the original object"); std.displayData(); System.out.println("Contents of the copied object"); Student copyOfStd = new Student().copyObject(std); copyOfStd.displayData(); } }
Output
Enter your name Krishna Enter your age 20 Contents of the original object Name : Krishna Age : 20 Contents of the copied object Name : Krishna Age : 20
- Related Articles
- Java Program to Pass ArrayList as the function argument
- What is the range of date time value that we can pass as an argument to MySQL UNIX_TIMESTAMP function?
- Java Program to pass lambda expression as a method argument
- How to pass an entire structure as an argument to function in C?
- Can we store objects in an array in Java?
- How to pass entire structure as an argument to function in C language?
- Can you pass the negative number as an Array size in Java?
- How to pass entire array as an argument to a function in C language?
- How to pass the address of structure as an argument to function in C?
- How to pass a dictionary as argument in Python function?
- Pass the Output of a Command as an Argument for Another on Linux
- How to pass Python function as a function argument?
- How to pass individual elements in an array as argument to function in C language?
- Can we declare an interface as final in java?
- How to pass the address of structure as an argument to function in C language?

Advertisements