# Recursion with static and non static methods

• 03-21-2009, 05:04 PM
sh4dyPT
Recursion with static and non static methods
Hello,

Can recursion be used with static and non-static methods?If so, what is the difference between using with one and another?

than you.
• 03-21-2009, 05:09 PM
Fubarable
Yes, it can be done with either static or non-static methods. The main difference is that one uses static methods and the other doesn't.

Something tells me that this isn't the answer you're looking for, but I think that if you're looking for a better answer, you're going to have to ask a much better question.
• 03-21-2009, 05:14 PM
sh4dyPT
So if I have a non static recursive method in my class, can I use "this." to refer a member of object?
• 03-21-2009, 05:40 PM
Fubarable
I think that's true of any non-static method, recursive or non, that you can refer to the current object with the key-word "this". I am curious as to what's this all about. There's some secret undercurrent that's driving these questions. What is it?
• 03-21-2009, 05:47 PM
sh4dyPT
No, I'm studying at university, this phrase "Recursive algorithms can be implemented only with static methods", I need to justify if it is true or false, but I don't understand well the difference between using recursion with static or no-static methods.
What is the difference?
• 03-22-2009, 03:27 PM
sh4dyPT
Can anyone explain me a difference between using recursion with static and non static methods?
• 03-23-2009, 07:57 AM
Webuser
as you can imagine, java static fields and static methods.
You see static methos must return something.
Code:

```public static String method(String text){ return text+" the text is returned with static method"; }```
void methods do not return anythig but execute some code like a:
Code:

```public void nonStatic(){ System.out.println("I am a non-static code :)"); }```
But method is also can be static and void at the same time :) look
It returns nothing but can be referenced from a static context
Code:

```public static void voidAndStatic(){ System.out.println("I am both :)"); }```
If they demand you to use static method they want to say that they need you to make a function which returns something or the method should be accessable from a static context...
And the most powerful thing of static methods is they allow to keep data in heap as long as the program is running. For example if you need to set an index of clicking a button and the method is in another class... you can use static field for this )
• 03-23-2009, 10:57 AM
sh4dyPT
So there is no difference between using recursion with static and nonStatic methods?
I'm a little bit confussed, for exemple this method -> "public void nonStatic()", How do you use it recursively if this method doesn't return anything? or it will become recursive by simply adding Base Case and invoke method iside the method?

thank you
• 03-23-2009, 12:08 PM
mtyoung
i think the difference is you need to create an owner object before calling nonStatic method and this may be used

while static method need not to create owner object and this can be used
• 03-23-2009, 09:57 PM
sh4dyPT
• 03-23-2009, 10:12 PM
Webuser
No there is a big difference. It is all fictional impression at first sight :)
If you 're use Swing and static JComponents in an applet you would see the multicopy or disappearing gui effects:)
static methods do not allow you to create separated method instances. The thing is connected to Java heap structure )

can you see difference...

Code:

```public class A{ public void methodA(){ System.out.println("I am void method A"); } }```
call method A from class B constructor...
Code:

```public class B{ public B(){ A a=new A(); a.methodA();/*can you predict result here?*/ } } //or you can get class A from class B as public class B extends A{ public B(){ this.methodA(); } }```
• 03-23-2009, 10:20 PM
Webuser
static fields keep data during the whole program execute.
well, as a rule, static fields progers use for constants
Quote:

static int CONST_FOR_PROGRAM=1;
static methods is almost the same thing as static fields (if not void static of course) because static method returns a value for a field )))
• 03-23-2009, 10:27 PM
Webuser
watch it
Code:

```public class A{ public static int staticMethod(int param){ int a=10; int b=b+a; int c=(param+b)/param; return c; } }```
ANd here you call the static method...
Code:

```public class B{ public static void main(String [] args){ int callStaticMethod=A.staticMethod(100); } }```
Can you see any difference =)))
• 03-26-2009, 07:26 PM
sh4dyPT
Wow, this is a good explanation... now this things make sense :). Thank you
• 03-27-2009, 07:56 AM
Webuser
I am glad that helped )