#Can a method itself be it's own return type??

67 messages · Page 1 of 1 (latest)

cursive steeple
#

How exactly does a return statement work??

as far as I have learned return statement returns a value from a method unless the method is not void,
now this value depends on type of the method, if the method is of String type it will return a string, if method type is int then returns and integer ..and so on

My doubt is how can a return statement return a method( ) itself ?
if it is doing so can we treat method( ) itself as it's own return type?

recursive method( ) below :

class abc{
    
    String method1(){
    System.out.println("Hellworld");
    return method1();
    
    }    
}

My full code :

package javaapplication6;

class abc{
    
    String method1(){
    System.out.println("Hellworld");
    return method1();
    
    }    
}

public class sample3{
public static void main(String arr[]){
     
    abc obj1=new abc();   
    obj1.method1();
       
    }
}



pulsar houndBOT
#

This post has been reserved for your question.

Hey @cursive steeple! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

cursive steeple
#

Also, In how many different ways can we play with return statements?

astral inlet
#

return method1(); this does not return method itself, it returns the result produced by method() call

cursive steeple
#

yea

#

but what is method1( ) in the satement return method1() acting as here?

eternal field
cursive steeple
#

yes it will

#

i know

#

but my question is not that

eternal field
#

Then I don't quite understand your question?

cursive steeple
#

Maybe I did not ask it clearly

#

wait

#

send me the syntax of return statement once

eternal field
#

return <returnValue>

#

?

cursive steeple
#

Then

#

return method1() ; method1() is a return value here?

eternal field
#

It will run method1

#

and return the value that method1 returns

cursive steeple
#

value or call ?

eternal field
#

call

#

You're calling the method and returning the result

cursive steeple
#

Then it voilates the classical syntax?

eternal field
#

?

cursive steeple
#

<return value>

eternal field
#

the return value would be the result of the method

#
return methodA();

Object a = methodA();
return a;
#

would do the same thing

cursive steeple
#

return <returnValue/method call>

cursive steeple
eternal field
#

Well that could be confusing for some

#

you're not exactly returning the method call you're returning the value of that method call

cursive steeple
#

very confusing indeed

#

Correct me if I am wrong or right or if I am missing something :

by typing this

return method1();

here method itself is acting as it's own value,
when the compiler encounters this call return method1(); it tries to return a value which is contained in that method for that the whole method is called including the return statement which again calls the method in expectation of returning a value....

#

This is like a infinite inner calls

eternal field
#

This would be handled at runtime not a compile time

#

but yes infinite calls

cursive steeple
#

call inside a call, inside a call, inside a call, inside a call, inside a call, inside a call

eternal field
#

exactly

#

it reaches return which calls method1 and that method1 reaches return and calls method1

#

etc

#

it will eventually lead to a StackOverflow

cursive steeple
#

infinite nested calls

cursive steeple
eternal field
#

yeah

cursive steeple
#

so here we are basically tricking a method to call itself? which again contains a self call?

#

is it?

eternal field
#

Well not tricking it? You're just telling it to do that

#

but other than that yes

#

it will call itself until interupted

cursive steeple
#

if stack memory is very small would it overflow pretty quickly? within fewer iterations / recursions?

eternal field
#

Yeah

#

why?

cursive steeple
#

1st tell me if it's possible or not?

eternal field
#

You can increase the stack size with the -Xss jvm flag

#

it does use RAM just not the heapspace you're thinking of

cursive steeple
#

ok

cursive steeple
#

Thanks @eternal field @astral inlet @quaint patrol

pulsar houndBOT
wet owl
#

i know this has probably already been answered, but calling method1() gets the value that method1 returns, and you can use that however you like. in this case, you return that value as well

#

you dont return a method, you return the value that method call returns

#

in your case, since the return value of method1 is dependent on the return value of method1, executing this just goes into an infinite loop