Monday 16 May 2011


Simple Java Program for Call by Reference and Returning

This code is shared by Umang Mistry (Vissanji Academy)X B.
This is a java program under the sub topic Function
This is to show an example on the topic calling (call by reference)& returning

Code:
/* Simple Java Program for Call by Reference and Returning */
public class chaining
{ public static int no1,no2;
   public static void main (int no11 , int no22)
   {
       no1=no11;
       no2=no22;
       chaining mobj = new chaining();
       int res=mobj.add(mobj);//call by reference ***
        System.out.println("I M from add in main ="+res);
    }
    public static int add (chaining mobj)//mobj is received ***
    {
        int result = mobj.no1+mobj.no2;
        chaining addobj = new chaining();
        int res=addobj.add1(mobj);//call by reference ***
       System.out.println("I M from add1 in add ="+res);
                return result;
    }
    public static int add1(chaining addobj)//addobj is recieved ***
    {
        int result11 = addobj.no1+addobj.no2;
        chaining add1obj = new chaining();
        int res=add1obj.add2(add1obj);//call by reference ***
        System.out.println("I M from add2 in add1 ="+res);
               return result11;
   
    }
    public static int add2(chaining add1obj)//add1obj is recieved ***
    {
        int result111 = add1obj.no1+add1obj.no2;
   
                System.out.println("I M  in add2 ");
               return result111;
    }
}

Comment below for your Query and Feedback... :)

Related Posts :



0 comments:

Confused? Feel free to ask

Post a Comment