Skip to main content

How to pass an object as an argument in Java programming?

We know that the concept of calling a method as pass by value and pass by reference. Java allows us to pass primitive types of data when calling a method that is called as 'a method call by value' or 'passed by value'. In the same manner, we can create an object by passing primitive type of data using constructors. 

Java allows us to create an object by passing reference of another object ie. One Object can be passed to another object. This is called as 'call by reference' or 'passed by reference'. 

Syntax

    <class name> <object name1>=new <class name>;
    <class name> <object name2> ;
    <object name2>=<object name1>;

Example Program

class test
{
    int i;
    test(int a)
    {
    i=a;
    }
    int testmethod(test t)
    {
    return(t.i+i);
    }
}
public class passobject
{
    public static void main(String[] s)
    {
        test t1=new test(50);
        test t2=new test(1);
        System.out.println(t2.testmethod(t1));
    }
}

In this example, 'test' is a class, and the values are assigned through the constructors. 'testmethod()' is a method that takes an object 't' as a parameter and returns the summation value of its local value 'i' and another object's value 't.i'. In the main class 'passobject', 't1' and 't2' are the objects of the 'test' class. The objects 't1' and 't2' assign 50 and 1 to the variable i. When calling 'testmethod()' it adds 50+1 and produces output as 51. 

Output


What is the advantage of using pass-by-reference over pass by-value?

Passing in by reference can be faster than by value, because when you pass an object by reference you are effectively passing just a pointer to the existing object, whereas passing by value makes a full copy of the object on the stack which is more expensive in terms of CPU time and memory.

How to return an object  in Java programming?

A primitive type's assigned value is passed to the method when it is passed as an argument. This means that the value is local to the method and any changes made to it by the method will not affect the value of the primitive you passed to the method.

As is common knowledge, a Java reference points to the memory location of an object when it is assigned to one; otherwise, the object is started as null. It is important to keep in mind that the memory location of the assigned object is represented by the value of the reference. Consequently, if we send a reference as an argument to any method, we are actually passing the memory address of the object that is assigned to that specific reference. This technically indicates that our constructed object is located in memory and accessible by the target method. Therefore, if the target method were to access our object and modify any of its properties, we would discover that the value of our original object has changed.

Example Program

class Sample
{
   private int value;
   Sample(int i)   
   {
      value = i;   
   }

   public Sample returnsample()   
  {
      Sample t = new Sample(value * 2);
      return t;   
   }

   public void show()   
   {
      System.out.println("Value : " + value);   
   }
}

public class ReturnObjectDemo
{
   public static void main(String[] args)   
   {
      Sample obj1 = new Sample(10);
      Sample obj2;
      obj2 = obj1.returnsample();
      obj2.show();   
   }
}

Comments

Popular posts from this blog

  Introduction to Java (For B.Tech/BCA Students) Java is a powerful, high-level programming language widely used in academic learning and professional software development. Developed in the mid-1990s, Java was designed with the goal of creating programs that can run on different types of computers without needing major changes. This platform independence is achieved through the concept of “write once, run anywhere,” making Java especially valuable in today’s diverse computing environment. For B.Tech students, Java serves as an excellent foundation for understanding core programming concepts. It fully supports object-oriented programming (OOP), including classes, objects, inheritance, polymorphism, abstraction, and encapsulation. These concepts are essential for building structured, reusable, and scalable software systems, which are central to modern computer science and engineering education. Java programs are compiled into an intermediate form called bytecode, which runs on th...
  Identifiers, Literals, Operators and Separators in Java Programming Language Here is a clear introduction (exam-ready + student-friendly) to Identifiers, Literals, Separators, and Operators in programming (especially Java/C-style languages). 🔹 Identifiers An identifier is the name given to a variable, method, class, array, or any user-defined item in a program. It helps identify these elements uniquely. ✔ Rules for Identifiers (Java) Must begin with a letter (A–Z or a–z), underscore (_), or dollar sign ($) Cannot start with a number Cannot be a reserved keyword Case-sensitive ( total and Total are different) No spaces allowed Examples ✔ Valid Identifiers int age; double totalMarks; String studentName; int _count; ❌ Invalid Identifiers int 2value;       // starts with number int total marks; // contains space int class;        // keyword 👉 ...