File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion in initializing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion in initializing" Watch "confusion in initializing" New topic
Author

confusion in initializing

Michael Lin
Ranch Hand

Joined: Dec 04, 2000
Posts: 31

class Initialize
{
String name;
int age;
Initialize[] account;

{
name="test";
age=33;
account= new Initialize[3];
account[2] = new Initialize();
} //instance initializer

public static void main(String[] args)
{
Initialize b= new Initialize();
System.out.println("name: " + b.name + " age: " + b.age
+ " account: " + b.account + " element: " + b.account[2]);
}//
}//

//this code compiles fine, but run time error!
//can anybody explain to me why??
//thanks
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

You are going into an endless loop and ultimately causing
a stack overflow here.


....
Rajni Singh
Greenhorn

Joined: Dec 05, 2000
Posts: 8
Sahir, will you please be more specific as to what causes the stack overflow ?
Michael, could you tell us the error please ?
Sahir Shah
Ranch Hand

Joined: Nov 05, 2000
Posts: 158

Every time he creates an object of this class it creates another object on this class which in turn creates another object of this class and so on. This is an endless loop. Hence the stack overflow. Try this out.
<pre>
class Gobble{
Gobble g = new Gobble();
}

public class Test{
public static void main(String[] args)
{
Gobble g = new Gobble();
}
}
</pre>

Rgds
Sahir
[This message has been edited by Sahir Shah (edited December 11, 2000).]
tony hutcheson
Ranch Hand

Joined: Dec 08, 2000
Posts: 38
I believe that more research should be done on this. I do not believe that Sahir is correct. Only one instance of the class is created, there is no constructor here, so recursive object genereation does not occur. Sorry I can't be more help.
Sahir,
my appologies if you can prove otherwise.
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
Sahir is correct. An infinite-length linked list of objects is created.
tony hutcheson
Ranch Hand

Joined: Dec 08, 2000
Posts: 38
I am sorry for disagreeing. I now see that the line inthe static initialization block:
account[2] = new Initialize();
is called every time a new instance if Initialize is created.
It would be helpful if those posting replies would explain why something is right or wrong. I appreciate the discussion and what I learn from it at javaranch.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Sahir is correct. Look at the two versions of this code below. In the first version, I have a static variable that I initialize to 0. Then I check to see if it is less than 10 in a block, if it is, I create a new Initialize object in the first version, and then increase the static variable.

This will give a stack overflow runtime error, becuase age is always less than 10, so you create an infinte number of Initialzie objects.
Now switch the order of the age increment in the block so you increase age and then create a new initialize object and it will compile with age being equal to 10, and one new Initialze object name b1.

This shows that the block gets run once everytime an object is created. If this was a static block, then it would only run once, the first time the class is loaded.
Bill
[This message has been edited by bill bozeman (edited December 11, 2000).]
 
jQuery in Action, 2nd edition
 
subject: confusion in initializing