| Author |
doubt pls!!
|
vivek sivakumar
Ranch Hand
Joined: Aug 09, 2001
Posts: 187
|
|
public class teststatic { public teststatic() { } static void test(){ System.out.print("im stable"); test(); } static void test1(){ System.out.print("im stable"); } public static void main(String[] args) { teststatic teststatic1 = new teststatic(); Object myob = new Object(); teststatic.test(); class inn{ } } } if i run this stuff then the word im stable is printed in like a never ending loop!!! any suggestions.
|
SCJP, SCWD <br />A farmer learns more from a bad harvest than a good one.
|
 |
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
|
|
|
your static method test() is invoking itself recursively...
|
SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
|
 |
Hima Nadi
Ranch Hand
Joined: Apr 08, 2002
Posts: 32
|
|
Hi Vivek try this public class teststatic { public teststatic() { } static void test(){ System.out.print("im stable"); //test(); } static void test1(){ System.out.print("im stable"); } public static void main(String[] args) { teststatic teststatic1 = new teststatic(); Object myob = new Object(); teststatic.test(); class inn{ } } } now it wont go in ever ending loop Hima
|
 |
vivek sivakumar
Ranch Hand
Joined: Aug 09, 2001
Posts: 187
|
|
no my doubt is that why is it calling itself in a never ending loop , i didnt specify it to be recursive nor did i give any kind of never ending for(; loops. i just called a methos inside another method.
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Indeed, you are just calling a method from within another method. In just so happens that you're calling the same method from within that method. Your program executes the test() method. Then, while executing that method, the test() method is invoked again. This results in test() being executed again...and again...and again. Each time you call test(), it calls itself. In Java, you don't declare methods to be recursive. There is nothing different about a recursive method than any other except that they execute themselves somewhere within their own body. I hope that helps, Corey
|
SCJP Tipline, etc.
|
 |
vivek sivakumar
Ranch Hand
Joined: Aug 09, 2001
Posts: 187
|
|
Originally posted by Corey McGlone: Indeed, you are just calling a method from within another method. In just so happens that you're calling the same method from within that method. Your program executes the test() method. Then, while executing that method, the test() method is invoked again. This results in test() being executed again...and again...and again. Each time you call test(), it calls itself. In Java, you don't declare methods to be recursive. There is nothing different about a recursive method than any other except that they execute themselves somewhere within their own body. I hope that helps, Corey
Thanks a lot corey for ur support
|
 |
 |
|
|
subject: doubt pls!!
|
|
|