A Recursive Class

A

A. Meyer

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
 
J

Josef Pfleger

A. Meyer said:
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

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; }
 
P

Patricia Shanahan

A. Meyer said:
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;

The "= null" is superfluous, but harmless.
public boolean isTestNull(){
if (this.myTest==null)
return true;
else
return false;

Why not just "return myTest==null;"?
}

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

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
 
K

KiLVaiDeN

A. Meyer said:
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
 
W

Wibble

KiLVaiDeN said:
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.
 
P

Patricia Shanahan

Wibble said:
KiLVaiDeN wrote: ....

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,083
Members
48,773
Latest member
Kaybee

Latest Threads

Top