为什么在我尝试在其自身类中创建一个对象时显示堆栈溢出错误?

huangapple go评论76阅读模式
英文:

Why is it showing stackoverflowerror when i am trying to create an object in it's own class?

问题

package inheritance;

public class SingleInheritance {

//	SingleInheritance obj=new SingleInheritance();     Why does this line is not giving any error when I am creating a class's object in its own class

	public static void main(String[] args) {
		Plumber rahul=new Plumber();   

	}

}
package inheritance;

class Plumber{

	Plumber ganesh=new Plumber();  
    // while  this one is giving the stackoverflowerror.
}

这行代码在同一个类中创建该类的对象时并不会产生错误:

SingleInheritance obj = new SingleInheritance();

但是在另一个类中创建该类的对象时会抛出堆栈溢出错误(stackoverflowerror):

Plumber ganesh = new Plumber();

我知道在自己的类中创建对象有些愚蠢,但这是我在尝试其他操作时发生的。我需要解释一下发生了什么。

英文:
package inheritance;  

public class SingleInheritance {

//	SingleInheritance obj=new SingleInheritance();     Why does this line is not giving any error when I am creating a class's object in it's own class

	public static void main(String[] args) {
		Plumber rahul=new Plumber();   
		
		}

}
package inheritance;

class Plumber{
	
	Plumber ganesh=new Plumber();  
        // while  this one is giving the stackoverflowerror.
	}

It does not throws any error when I create the object of SingleInheritance class in it's own class but throws an error while I do the same thing in another class .为什么在我尝试在其自身类中创建一个对象时显示堆栈溢出错误?
I know It is silly to create object in it's own class but this happened when I was trying to do something else. I need an explanation to what is happening .

答案1

得分: 1

你的代码问题在于类Plumber的对象被递归地创建,并且没有终止递归的条件。

让我们看一下你的类Plumber的内容及其实例化。

class Plumber
{
   Plumber obj = new Plumber();
}

你认为在创建一个new Plumber()对象时会发生什么?
它会实例化一个new Plumber()并赋值给obj,然后会在obj.obj中再次创建一个new Plumber(),依此类推...

当然你可以在同一个类中保留一个Plumber对象,但在实际初始化它时需要有一个特定的流程。

class Plumber
{
   Plumber obj;
   public Plumber()
   {
      if(/*条件*/)
      {
         obj = new Plumber();
      }
   }

   // 你也可以使用一些方法来实现
   public void InstantiateObj()
   {
      obj = new Plumber();
   }
}
英文:

The issue with your code is that there is recursively creation of your class Plumber objects and no condition that will terminate it.

Let us see the contents of your class Plumber and their instantiation.

class Plumber
{
   Plumber obj = new Plumber();
}

What do you think this does on creating a new Plumber() object.
It will instantiate a new Plumber() to obj, which will in return create another new Plumber() to obj.obj, and so on ..

You sure can keep an object a Plumber in same class, but you need to have a specific flow when you want to actually initialize it.

class Plumber
{
   Plumber obj;
   public Plumber()
   {
      if(/*condition*/)
      {
         obj = new Plumber();
      }
   }

   // You can also use some methods to do so
   public InstantiateObj()
   {
      obj = new Plumber();
   }
}

答案2

得分: 1

这是因为您没有实例化 SingleInheritance 类。
以下是更改后的代码部分:

public class SingleInheritance {

    SingleInheritance obj=new SingleInheritance(); 

    public static void main(String[] args) {
        Plumber rahul=new Plumber();   
    }
}

原因是这段代码没有创建 SingleInheritance 的新实例,因为 main 是一个静态函数。

如果您将代码更改为:

public class SingleInheritance {

    SingleInheritance obj=new SingleInheritance(); 

    public static void main(String[] args) {
        SingleInheritance rahul=new SingleInheritance();   
    }
}

您将会得到相同的堆栈溢出异常,因为现在 main 将会实例化 SingleInheritance。导致堆栈溢出的原因是,new Plumber() 调用了它自己的构造函数,就像其他答案中解释的那样。

英文:

This is because you are not instantiating SingleInheritance class.
The code

public class SingleInheritance {

    SingleInheritance obj=new SingleInheritance(); 

    public static void main(String[] args) {
        Plumber rahul=new Plumber();   
    }
}

Is not creating a new instance of SingleInheritance because main is a static function.

If you change your code to:

public class SingleInheritance {

    SingleInheritance obj=new SingleInheritance(); 

    public static void main(String[] args) {
        SingleInheritance rahul=new SingleInheritance();   
    }
}

You will get the same Stackoverflow exception because now main will instantiate SingleInheritance. The reason you get Stackoverflow is that new Plumber() calls its own constructor like other answers do explain.

huangapple
  • 本文由 发表于 2020年9月2日 13:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63699201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定