Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is wrong with the code??

 
gourab
Greenhorn
Posts: 1
  • 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
Posts: 42968
73
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic