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;
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.
Your incrByTen() method creates an instance of the Test class and returns it so it's assigned to the reference "ob2". Hence you can use any methods from the object referred by "ob2:
And please UseCodeTags when posting code. Unformatted code makes it hard to read the post.
Thank you very much.
Sorry for the inconvenience. Sorry to ask but how do I do usecodetags while posting a query. I'm using my iPhone at the moment and don't have access to a PC for now. I do apologise if I should have known it by now.