Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

A Recursive Class

Thread view: 
A. Meyer - 09 Oct 2006 15:30 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.