Java-Objects as Parameters

Objects as Parameters

A method can take an objects as a parameter. For example, in the following program, the method setData( ) takes three parameter. The first parameter is an Data object. If you pass an object as an argument to a method, the mechanism that applies is called pass-by-reference, because a copy of the reference contained in the variable is transferred to the method, not a copy of the object itself.



Program Source

class Data {
    int data1;
    int data2;

class SetData {
    void setData(Data da,int d1,int d2)
        da.data1 = d1;
        da.data2 = d2;
    void getData(Data da)
        System.out.println("data1 : "+da.data1);
        System.out.println("data2 : "+da.data2);

public class Javaapp {
    public static void main(String[] args) {
        Data da = new Data();
        SetData sd = new SetData();

Leave a Comment