Login    New User    Forgot Password    Help      

Returning object ( Constructor)

Java Tutorials

Example

class abc
{
int a;
int b;
int sum;

abc sum(abc obt)
{
obt.sum=obt.a+obt.b;
return obt;
}

}
class clas8
{
public static void main(String args[])
{
abc ob1=new abc();
abc ob2=new abc();

ob1.a=10;
ob1.b=20;

 

ob2=ob2.sum(ob1);
System.out.println("Value of a is " +ob2.a);
System.out.println("Value of b is" +ob2.b);
System.out.println("Sum  of a and b is" +ob2.sum);

}
}

Output

e:\java\java clas8

Value of a is 10

Value of b is 20

Sum of a and b is 30

Java Tutorials