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

Thread: Static Function

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Static Function

    public class Static_and_nonstatic {
    String name;
    static int age;
    public static void main(String[] args)
    {
    age = 26;
    sum();
    }
    public static void sum()
    {
    int x = 100;
    age = 26;
    sum(); //WHY IT IS DISPLAYING ERROR IN THIS LINE.
    }
    public void sendMail()
    {
    int y = 300;
    name = "Vinay";
    age = 26;
    sum();
    sendMail();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Static Function

    Please copy and paste here the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Static Function

    Your methods have no logic to them. Basically you're making a recursive call to a method that will never break out of the loop.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Static Function

    public void sendMail()
    {
    int y = 300;
    name = "Vinay";
    age = 26;
    sum();
    sendMail(); // It is also recursive but not showing any error.
    }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Static Function

    Where is the error message that you are talking about?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Static Function

    Quote Originally Posted by vinaysa86 View Post
    public class Static_and_nonstatic {
    String name;
    static int age;
    public static void main(String[] args)
    {
    age = 26;
    sum();
    }
    public static void sum()
    {
    int x = 100;
    age = 26;
    sum(); //WHY IT IS DISPLAYING ERROR IN THIS LINE.
    }
    public void sendMail()
    {
    int y = 300;
    name = "Vinay";
    age = 26;
    sum();
    sendMail();
    }
    }
    This Code Will not give any error at compile time.
    But at the time of running this code sum() will called recursively and a never ending process until unless you did not stop the execution explicitly or any MemoryOutOfBound exception occurs.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Location
    INDIA
    Posts
    2
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Static Function

    you are running into an infinite loop.... method sum() has no end..
    And sendMail() method is never invoked in main(), hence you got no error there.

Similar Threads

  1. non static varible from static context error
    By chopficaro in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 5th, 2012, 07:07 PM
  2. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM