class Test{
int a;
Test(int i){
a=i;
}
Test incrByTen(){
Test temp=new Test(a+10);
return temp;
}
}
class RetOb{
public static void main(
String args[]){
Test ob1=new Test(2);
Test ob2;
ob2=ob1.incrByTen();
System.out.println("ob1.a: "+ob1.a);
System.out.println("ob2.a: "+ob2.a);
ob2=ob2.incrByTen();
System.out.println("ob2.a after second increase: "+ob2.a);
}
}
Output
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22
If objects have to be allocated first using 'new' before any attempt to use them in the program. Then how do these lines of code work
ob2=ob1.inceByTen();
ob2=ob2.inceByTen();
as ob2 hasn't been allocated at all.
Can anybody please explain this