Passing Objects to Methods in Java for ISC Class 12 (Object as Parameter and Return Type)
ISC Class 12 Computer Science notes on object passing: passing an object as a parameter, returning an object from a method, using this to access the current object, and a fully solved board-style Distance class program.

Many ISC Section B questions follow one pattern: "design a class… with a method that accepts an object of the same class and returns an object". Examples are adding two times, merging two arrays, or comparing two dates. Once you understand how objects are passed, these questions become routine.
How Java passes objects
When you pass an object to a method, Java passes a reference to it. The method works on the same object, so changes it makes to the object's data members are visible to the caller.
class Box {
int size;
}
static void grow(Box b) {
b.size = b.size * 2; // changes the caller's object
}
Box x = new Box();
x.size = 5;
grow(x);
System.out.println(x.size); // 10Compare this with primitives (int, double and so on), where the method gets a copy and the original never changes.
The current object and the parameter object
Inside a method such as obj1.add(obj2):
- the data members written on their own (or as
this.feet) belong to obj1, the current object; obj2.feetbelongs to the parameter object.
Returning an object
A method can create a new object, fill it in and return it:
Distance add(Distance d) {
Distance t = new Distance();
// ... fill in t ...
return t;
}Solved board-style program: class Distance
Question: Design a class Distance with these members:
- Data members:
int feet,int inches - Distance(): default constructor that sets both to 0
- Distance(int f, int i): parameterised constructor
- Distance add(Distance d): returns a new object holding the sum of the current distance and d, with inches adjusted so they are less than 12
- boolean isLonger(Distance d): returns true if the current distance is longer than d
- void show(): prints the distance as feet' inches"
Write a main() method to add 5' 8" and 3' 7".
class Distance {
int feet, inches;
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
Distance add(Distance d) {
Distance t = new Distance();
int totalInches = this.inches + d.inches;
t.inches = totalInches % 12;
t.feet = this.feet + d.feet + totalInches / 12;
return t;
}
boolean isLonger(Distance d) {
return (feet * 12 + inches) > (d.feet * 12 + d.inches);
}
void show() {
System.out.println(feet + "' " + inches + "\"");
}
public static void main(String args[]) {
Distance d1 = new Distance(5, 8);
Distance d2 = new Distance(3, 7);
Distance d3 = d1.add(d2);
d3.show(); // 9' 3"
System.out.println(d1.isLonger(d2)); // true
}
}How the sum works: 8 + 7 = 15 inches, which is 1 foot and 3 inches. Feet = 5 + 3 + 1 = 9, so the result is 9' 3".
A second common pattern: merging two arrays into one object
ISC questions often wrap an array in a class and ask for a method that merges two objects:
class Collection {
int arr[];
int len;
Collection(int n) {
len = n;
arr = new int[n];
}
Collection merge(Collection c) {
Collection r = new Collection(this.len + c.len);
for (int i = 0; i < this.len; i++)
r.arr[i] = this.arr[i];
for (int i = 0; i < c.len; i++)
r.arr[this.len + i] = c.arr[i];
return r;
}
}Exam tips
- Create the result object with
newinside the method. Don't modifythisunless the question says the current object should change. - Use
this.memberandparam.memberconsistently, so the examiner can see which object you mean. - Always write the return type as the class name (for example
Distance add(Distance d)) and finish withreturn. - In
main(), show the call in the formobj3 = obj1.method(obj2);. Examiners look for it.
Practice questions
- What is printed if
grow(x)above instead setsb = new Box(); b.size = 50;? - Write a method
Time addTime(Time t)that adds two times given in hours and minutes. - Why must a method that returns an object create it with
new?
Answers:
- 5. The method points
bat a new object, so the caller's object is untouched. - Add the minutes, carry
total / 60into the hours, and keeptotal % 60as the minutes. The structure is the same asadd()above. - Otherwise there is no object to return. Returning
thiswould hand back, and possibly change, the caller's own object instead of a separate result.
Revise Inheritance, Abstract Classes and Interfaces and Stack, Queue and Linked List to complete the ISC Class 12 Java syllabus.
Related Posts
Continue learning with these related articles

Arrays in Java for ISC Class 11: 1-D Arrays and 2-D Matrix Programs
ISC Class 11 Computer Science notes on arrays in Java: declaring 1-D and 2-D arrays, row-major and column-major address calculation, and solved matrix programs – transpose, boundary sum, diagonal sums and matrix multiplication – for theory and practicals.

Stack, Queue and Linked List in Java for ISC Class 12 (Notes and Programs)
ISC Class 12 Computer Science notes on data structures: stacks (LIFO) and queues (FIFO) using arrays with overflow and underflow, postfix evaluation, and singly linked lists – insert, traverse, count and search – with board-style Java programs.

Logic Gates for ISC Class 11: Truth Tables, Universal Gates, Half and Full Adder
ISC Class 11 Computer Science notes on logic gates: AND, OR, NOT, NAND, NOR, XOR and XNOR with truth tables, why NAND and NOR are universal gates, and half adder and full adder circuits with their Boolean expressions.