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
Josef Pfleger - 09 Oct 2006 15:52 GMT
> Hello everybody,
>
[quoted text clipped - 27 lines]
> Thx in advance
> Amir
Wether or not this is "good prgramming style" depends on your problem.
It is correct Java Syntax and sometimes members of the enclosing class's
type make perfect sense (linked lists come to mind).
ps: public boolean isTestNull() { return myTest == null; }
Patricia Shanahan - 09 Oct 2006 15:52 GMT
> Hello everybody,
>
[quoted text clipped - 6 lines]
> public class Test {
> private Test myTest = null;
The "= null" is superfluous, but harmless.
> public boolean isTestNull(){
> if (this.myTest==null)
> return true;
> else
> return false;
Why not just "return myTest==null;"?
> }
>
[quoted text clipped - 11 lines]
> Thx in advance
> Amir
Whether it is good programming style for your problem depends, of
course, on the problem.
However, it is a normal technique.
Consider a node in a binary tree:
class TreeNode{
private TreeNode leftChild;
private TreeNode rightChild;
...
}
Patricia
KiLVaiDeN - 09 Oct 2006 16:28 GMT
> A. Meyer wrote:
>
[quoted text clipped - 29 lines]
> 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
Wibble - 10 Oct 2006 12:34 GMT
>> A. Meyer wrote:
>>
[quoted text clipped - 48 lines]
> 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.
Patricia Shanahan - 10 Oct 2006 13:54 GMT
...
> 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.
Only if it is called simultaneously for the same object in different
threads. It is in a member method, not e.g. a static factory method.
Patricia