Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help understanding recrursive and static methods

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need help understanding recrursive and static methods

    I'm having trouble understanding the concepts of static methods, and how to call them. Also how recursive methods work through.

    for example, take this code:

    public static void recur2 (int n)
    {
      if(n<=0)
      { 
        return;
      }
      else
      {
        recur2(n - 2);
        System.out.print(n);
      }
    }

    what does the method call recur2(5) display? Furthermore, this would be easy to just test and see what the result is, but I don't know how to call static methods.

    Normally I would create some class and add the line "public static void main(String[] args)" which would be my driver class for whatever class I created to do some problem. So by that method I would create these two classes:

    public class Test{
     
      public static void recur2 (int n)
    {
      if(n<=0)
      { 
        return;
      }
      else
      {
        recur2(n - 2);
        System.out.print(n);
      }
    }
     
    }

    public class TestDriver{
      public static void main(String[] args){
        System.out.println(recur2(5));
      }
    }

    But that gives me an error saying this recur2(int) is undefined for the type TestDriver


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help understanding recrursive and static methods

    You need to specify in what context that method exists. Static simply means it exists in the context of the class itself, not any particular instance of that class. You can quantify this by qualifying the method with the class name.

    public class TestDriver{
      public static void main(String[] args){
        // recur2 belongs to the Test class
        System.out.println(Test.recur2(5));
      }
    }

Similar Threads

  1. Having trouble understanding recursive methods??
    By orbin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 17th, 2012, 12:08 AM
  2. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  3. When to use static methods?
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: June 7th, 2012, 01:51 AM
  4. Static Methods Problem
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 09:09 AM
  5. Help me understand when to use static on functions/methods
    By thenk24 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 1st, 2011, 11:42 AM