I am new to java programmming. Today this example is tought by my teacher in non static method. Any body tell me detailed answer from the point of jvm architecture
class Example {
int x = 10;
int y = 20;
Example e1 = new Example();
public static void main(String[] args){
System.out.println("main method start");
Example e2 = new Example();
System.out.println("main method end");
}
}
class Example {
int x = 10;
int y = 20;
Example e1 = new Example(); // this is the reason
What is happening is when you call new Example in your main() method, Example e1=new Example();
is being executed.
In Example e1=new Example();
you are again creating a new object of Example()
which will again call new Example()
(recursive calls to new Example()
leads to StackOverflowError
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.