Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Monday 18 July 2011

Program to Return the Object to main class in Java

     This program returns the object to the main class from the sub class.

import java.io.*;
class robject
{
int a;
robject(int x)
{

a=x;
}
robject test()
{
robject x=new robject(a+10);
return x;
}

}
class retobj
{
public static void main(String args[])
{
robject o2;
robject o1=new robject(5);
o2=o1.test();
System.out.println("Before Returning Object: "+o1.a);
System.out.println("After Returning Object: "+o2.a);
}
}
Output

Before Returning Object: 5
After Returning Object: 15

No comments:

Post a Comment

Followers