When we try to execute java program which will in infinite loop, we will get Stack Over flow exception as shown below.
package com.sample.java.testing;
public class ConstructorExample {
/**
* It gets stack overflow exception why because it continuously calls the
* constructor.
*/
private ConstructorExample() {
ConstructorExample constructorExample = new ConstructorExample();
System.out.println(constructorExample);
}
/**
* @param args
*/
public static void main(String[] args) {
new ConstructorExample();
}
}
Output:
Exception in thread "main" java.lang.StackOverflowError
at com.sample.java.testing.ConstructorExample.<init>(ConstructorExample.java:10)
at com.sample.java.testing.ConstructorExample.<init>(ConstructorExample.java:10)
package com.sample.java.testing;
public class ConstructorExample {
/**
* It gets stack overflow exception why because it continuously calls the
* constructor.
*/
private ConstructorExample() {
ConstructorExample constructorExample = new ConstructorExample();
System.out.println(constructorExample);
}
/**
* @param args
*/
public static void main(String[] args) {
new ConstructorExample();
}
}
Output:
Exception in thread "main" java.lang.StackOverflowError
at com.sample.java.testing.ConstructorExample.<init>(ConstructorExample.java:10)
at com.sample.java.testing.ConstructorExample.<init>(ConstructorExample.java:10)
No comments:
Post a Comment