Java: object creation causes runtime error

霸气de小男生 提交于 2019-12-13 07:49:06

问题


I am new to Java and I am trying some examples to understand how it works.

I am having problems understanding why the following code fails. I know the line that causes the error but I can't tell why. I made two classes, Class1 and Main, whose code is written in two separate .java files:

public class Class1
{
    int var;
    public void method1 ()
    {
        System.out.println(var);
    }
    Class1 obj1 = new Class1(); // this is the line that causes the error
}

and

public class Main
{
    public static void main (String[] args)
    {
        Class1 obj = new Class1();
        obj.method1();
    }
}

It compiles fine, but when I run java Main it just prints hundreds of times the error

at Class1.<init>(Class1.java:8)

I tried running java Main | more (I am using Unix Bash) but the pipe gets somehow ignored and I can't see the first line of the error message. Nor does java Main > log.txt output redirection to a text file work. If I remove that line, i.e. if I don't create the Class1 object obj1 in the Class1 class body, everything works fine. Can anyone explain to me what's wrong with that line?

Thank you


回答1:


The problem is you are creating Class1 object in an infinite loop.

When you create an Class1 object in main method, it initializes all variables you have defined in Class1 body. But since you also create a new Class1 object inside of Class1 body, it keeps creating Class1 object and at some point, it will cause stack overflow error.




回答2:


I think you have a recursive Object initialization.

Class1 obj = new Class1(); is an instance statement, so it's called at every instance, recursively, initiated from your main method.

This will cause a StackOverflowError.

The StackOverflowError is the JVM's way of telling you you are overflowing the stack memory, and the easiest way to trigger one is through infinite method or constructor recursion, such as in your case.




回答3:


That is effectively an infinite loop there in that you create a new instance of the class inside every new instance of the class eventually Java runs out of memory or stack and throws an exception.




回答4:


You code will eventually throw:

java.lang.StackOverflowError

Because of unconditional recursive initialization attempts.

To complete class construction it needs to complete this initialization:

Class1 obj1 = new Class1(); 

Which in turn will try to construct Class1 again and this vivacious cycle will continue until JVM give up with java.lang.StackOverflowError.




回答5:


this is illegal to do Class1 obj1 = new Class1(); at the class level of the same class

you should not create the object in the class level of the same class.its allowed to create reference like Class1 obj1; at class level of the same class



来源:https://stackoverflow.com/questions/19113616/java-object-creation-causes-runtime-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!