This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt pls!!

 
vivek sivakumar
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • 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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your static method test() is invoking itself recursively...
 
Hima Nadi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic