0% found this document useful (0 votes)
4 views1 page

Java Pass By Reference Example

This Java code defines a DemoPassByReference class with an age field and constructor. It has a display method that prints the ages of the passed in and current objects. The main method creates two DemoPassByReference objects and calls display on the second object, passing itself.

Uploaded by

Amresh Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Java Pass By Reference Example

This Java code defines a DemoPassByReference class with an age field and constructor. It has a display method that prints the ages of the passed in and current objects. The main method creates two DemoPassByReference objects and calls display on the second object, passing itself.

Uploaded by

Amresh Patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

public class DemoPassByReference {

// instance member
int age;

// parameterized constructor
DemoPassByReference(int age){
[Link] = age;
}

// method to display age


void display(DemoPassByReference objectParam){
[Link]("this - age : "+[Link]); // this > pointing >
CURRENT OBJECT
[Link]("object param - age : "+[Link]);
}

public static void main(String[] args) {


DemoPassByReference obj1 = new DemoPassByReference(10); // object 1
created, age = 10
DemoPassByReference obj2 = new DemoPassByReference(20); // object 2
created, age = 20

// [Link](obj1);
// [Link](obj2);

// [Link](obj1);
[Link](obj2);
}
}

/**
* types of member
* access modifier
* Encapsulation (OOPs concept) - setter & getter
* this keyword
* constructor
* PassByReference
*/

You might also like