Here is a Java Program to Demonstrate the Passed by Reference.
class Test { int a, b; Test(int i, int j) { a = i; b = j; } // pass an object void meth(Test o) { o.a *= 2; o.b /= 2; } }
Here is a Main Class to run the Above Class
class CallByRef { public static void main(String args[]) { Test ob = new Test(15, 20); System.out.println("ob.a and ob.b before call: " + ob.a + " " + ob.b); ob.meth(ob); System.out.println("ob.a and ob.b after call: " + ob.a + " " + ob.b); } }
Output of Above Java Program
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 10