Program to Return the Object to main class in Java

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

Creating Objects

Creating Objects                  Creating objects means to allocate memory space for all the instance variables of the objects. S...