• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt pls!!

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your static method test() is invoking itself recursively...
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
vivek sivakumar
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic