Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt pls!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt pls!!" Watch "doubt pls!!" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt pls!!
 
Similar Threads
Three interesting questions from javacross exam
There are some things that only the Creator(s) can explain !
Singleton pattern
static variables are not accesed by class instances????
k&b master exam doubt