aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is wrong with the code?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "what is wrong with the code??" Watch "what is wrong with the code??" New topic
Author

what is wrong with the code??

gourab
Greenhorn

Joined: Feb 12, 2008
Posts: 1
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
Marshal

Joined: Mar 22, 2005
Posts: 35220
    
    7
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.


Android appsImageJ pluginsJava web charts
seshu Palamanti
Ranch Hand

Joined: Jul 03, 2004
Posts: 54
Yea, Your MyClass a = new MyClass(); will create more objects in a cycle.
[ May 21, 2008: Message edited by: seshu Palamanti ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: what is wrong with the code??
 
Similar Threads
Anonymous class behavior confusion !
java.lang.NoSuchMethodError: main - What am I doing wrong?
new instance
Overriding rules... Please confirm!
Number of Objects eligible for Garbage Collection