function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anil kumar devunoorianil kumar devunoori 

We can invoke NON VOID method as VOID method. Interesting!

public class ApexMethodBehaviourTest {
    
    public static Integer staticNum = 0;
    
    public static Integer getNumber1(Integer i1){
        staticNum = i1;
        return i1;
    }
    
    public static Integer getNumber2(Integer i2){
        getNumber1(i2);
        return staticNum;
    }
    
    public Integer getNumber3(Integer i3){
        return i3;
    }
    
    public Integer getNumber4(Integer i4){
        getNumber3(i4);
        return i4;
    }
    
}

Today when I was reviewing the code, I observed that we can invoke a non void method as void method. It's interesting!
getNumber1(i2)  and getNumber3(i4) methods are non void but invoked as void method.
Best Answer chosen by anil kumar devunoori
Raj VakatiRaj Vakati
It will be collected by Garbalge collecter as there no vaibale to store getNumber3(i4); and it will return i4 whihc you are passing from the args 

Hope this solve your quesations .. 
 

All Answers

Raj VakatiRaj Vakati
Yes 

You can invoke  Both the ways .. For Compiler its just matter for the Last statement as the return in case if your outer method is having the return type 

Return method can invoke from nonreturn method and vice versa 
 
anil kumar devunoorianil kumar devunoori
Sorry the above comment was posted accidentally...
Raj, Thank you very much for reply and the interest you showed.

I am still having queries.

Generally in C and Java, if a method returns value, it should be invoked as below:
In case of method getNumber3:

public Integer getNumber3(Integer i3){
return i3;
}

It is generally invoked as below:

Integer i5=getNumber3(1);

But in Apex, I can invoke like void method as below:
 
getNumber3(1); //this doesnt work in C or Java as per my knowledge but its fine in Apex

How the compiler handles it? and where the value returned in statement getNumber3(1); goes?
Raj VakatiRaj Vakati
inJava you Cannot  But in salesforce its differnet ,..in Apex Compoler will collect the getNumber3(1); and its assign to interger i5 .. 
anil kumar devunoorianil kumar devunoori
Thanks again for the reply. You are awesome.

In below statement there is no assignment to i5:

getNumber3(1); //this statement doesnt work in C or Java as per my knowledge but its fine in Apex

same question:
where the value returned in statement getNumber3(1); goes?
Raj VakatiRaj Vakati
It will go to Integer i5 and assign to i5 local variable 
anil kumar devunoorianil kumar devunoori
Sorry! there is no i5 variable defined here in below code:
public class ApexMethodBehaviourTest {
    
    public static Integer staticNum = 0;
    
    public static Integer getNumber1(Integer i1){
        staticNum = i1;
        return i1;
    }
    
    public static Integer getNumber2(Integer i2){
        getNumber1(i2);
        return staticNum;
    }
    
    public Integer getNumber3(Integer i3){
        return i3;
    }
    
    public Integer getNumber4(Integer i4){
        getNumber3(i4);
        return i4;
    }
    
}

The highlighted statement(method invoke) generally returns a value. where that value returned by statement getNumber3(i4); goes? 
Raj VakatiRaj Vakati
It will be collected by Garbalge collecter as there no vaibale to store getNumber3(i4); and it will return i4 whihc you are passing from the args 

Hope this solve your quesations .. 
 
This was selected as the best answer
anil kumar devunoorianil kumar devunoori
Great. Thanks again for your help!

But still have questions
Why salesforce allowed to call NON VOID method like VOID method?
Raj VakatiRaj Vakati
No IDEA  and no where it was documented too .. 
anil kumar devunoorianil kumar devunoori
Let me post this question as new post on forum. Thanks Raj for your valuable time and it is nice discussing with you.
Raj VakatiRaj Vakati
Post to Stack exchange may be some one will help you  
anil kumar devunoorianil kumar devunoori
I have posted as new question on this forum.
https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005h7pQAA
If you get any information, please post there.
I dont have account with Stack Exchange. As suggested by you, I will post it on Stackewxchange too once I create one account for me. Have a good day!