File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is wrong with the code??

 
gourab
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while i am running this code i got a run time exception saying
Exception in thread "main" java.lang.StackOverflowError

Could some body help me out?

public class MyClass {

MyClass a = new MyClass();
public static void main(String[] args) {
// TODO Auto-generated method stub
MyClass b = new MyClass();
System.out.println("Inside main witout error");
}

}
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each instanceof MyClass that gets created in turn creates another instance of MyClass. That will up the stack fast, since there is no end to this object creation.
 
seshu Palamanti
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, Your MyClass a = new MyClass(); will create more objects in a cycle.
[ May 21, 2008: Message edited by: seshu Palamanti ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic