Re: A Recursive Class



KiLVaiDeN wrote:
A. Meyer wrote:

Hello everybody,

I have a recursive problem, which I tried to solve by defining a class,
which contains an attribute whose type the class itsself. The
(test)-programm looks then as follows:


package main;

public class Test {
private Test myTest = null;

public boolean isTestNull(){
if (this.myTest==null)
return true;
else
return false;
}

public void modifyMyTest(){
myTest = new Test();

}

}



Ist this good prgramming style or is it a kind of dirty solution?
Anyway, I have tried the solution and it seems to work. Plz give me
feedback.

Thx in advance
Amir

Hi,

Several points :

1) Why use recursivity ? Can't you modify your code, to make it
sequential ? You can always manage a transformation from recursive to
sequential, because it prooves much faster and much less resource
consuming, I assume.

2) Your call stack will explode if you have too many recursive calls.
Therefore, read 1)

3) I believe that introducing a second class ( a Controller class ) to
your code, that would instantiate your class as long as needed is the
best way to go. Can you tell us what's the need for recursivity ?

Cheers,
K

Recursive may not be slower in java. Compilers can optimize tail
recursion. Some things can be placed on the stack that would
otherwise require heap and gc.

He doesn't have a recursive algorithm, just a class that generates instances of itself. This could be used for lazy generate and
test algorithms.

A controller class would be overkill here.

Be careful about multi-thread use of this class.

if (t.isTestNull()) t.modifyMyTest();

The above could create an extra instance if called
simultaneously from multiple threads.
.