Results 1 to 15 of 15
  1. #1
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    So if I have a non static recursive method in my class, can I use "this." to refer a member of object?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  5. #5
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    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?
    Last edited by sh4dyPT; 03-21-2009 at 08:30 PM.

  6. #6
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    Can anyone explain me a difference between using recursion with static and non static methods?
    Ubuntu, Linux for human Beings!

  7. #7
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    as you can imagine, java static fields and static methods.
    You see static methos must return something.
    Java 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:
    Java 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
    Java 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 )
    Last edited by Webuser; 03-23-2009 at 08:00 AM.

  8. #8
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    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
    Ubuntu, Linux for human Beings!

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    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

  10. #10
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    Very helpful, thanks!!
    Ubuntu, Linux for human Beings!

  11. #11
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    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...

    Java Code:
    public class A{
    
    public void methodA(){
    System.out.println("I am void method A");
    }
    }
    call method A from class B constructor...
    Java 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();
    }
    }

  12. #12
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    static fields keep data during the whole program execute.
    well, as a rule, static fields progers use for constants
    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 )))

  13. #13
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    watch it
    Java 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...
    Java Code:
    public class B{
    
    public static void main(String [] args){
    int callStaticMethod=A.staticMethod(100);
    }
    }
    Can you see any difference =)))

  14. #14
    sh4dyPT's Avatar
    sh4dyPT is offline Member
    Join Date
    Mar 2009
    Location
    German
    Posts
    17
    Rep Power
    0

    Default

    Wow, this is a good explanation... now this things make sense :). Thank you
    Ubuntu, Linux for human Beings!

  15. #15
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM
  3. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  4. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •