The Artima Developer Community
Sponsored Link

Java Answers Forum
problem with obj creation

1 reply on 1 page. Most recent reply: Dec 26, 2005 1:00 AM by Tapan kumar Rath

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
prithvi venigalla

Posts: 1
Nickname: puddu
Registered: Dec, 2005

problem with obj creation Posted: Dec 25, 2005 12:02 AM
Reply to this message Reply
Advertisement
I have compiled the following program ,

public class A{
A a=new A();
public static void main(String args[]){
A b=new A();
}
}

and i compiled it with out any problem.While running it
i am getting a infinite loop and stackOverFlowError.
Why i am getting infinite loop.


Tapan kumar Rath

Posts: 18
Nickname: papuni
Registered: Sep, 2005

Re: problem with obj creation Posted: Dec 26, 2005 1:00 AM
Reply to this message Reply
Instance variables are always initialized when object is created.So creating the same object as instance variable triggers an infinite loop.Same happens if you put the line
within a constructor.It doesn't matter if starting point for object creation is within main method of the same class or in another class.Below I am giving an illustration
public class A{
	public static int a1=1;
	A(){
		if(a1>=5)System.exit(1);
		a1++;
		System.out.println(a1);
		A a=new A();
	}
	public static void main(String args[]){
		A b=new A();
	}
}

Flat View: This topic has 1 reply on 1 page
Topic: need some help Previous Topic   Next Topic Topic: Heap Abortion in Memory

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use