Forums Register Login

reference variable

+Pie Number of slices to send: Send
public class one
{
static one o=new one();
one p=new one();
public static void main(String[] args){
}
}
why static and non-static reference variable should not be given togther in the code above ? ( if so given, it causes stack over flow error)
+Pie Number of slices to send: Send
It's the member

one p=new one();

that causes the problem. That means every time you construct a "one" object, when its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed. While its member variable "p" is being initialized, another "one" object will be constructed... and eventually there are so many half-executed constructors on the stack, that you get a stack overflow.
+Pie Number of slices to send: Send
but if one o=new one(); alone that doesn't cause problem?
why? even int this if another reference variable created in side main method, that will be in problem...why?
+Pie Number of slices to send: Send
Because a "one" constructs a "one" while initializing a member variable, any attempt to create a "one" will cause a stack overflow. In your code, it's the static one that gets created automatically when the class is loaded that kicks this process off, but it's not really part of the problem. If you remove that static, and just put "new one()" into main(), you'd get the same error.
+Pie Number of slices to send: Send
 

Originally posted by Ernest Friedman-Hill:
Because a "one" constructs a "one" while initializing a member variable, any attempt to create a "one" will cause a stack overflow. In your code, it's the static one that gets created automatically when the class is loaded that kicks this process off, but it's not really part of the problem. If you remove that static, and just put "new one()" into main(), you'd get the same error.




You mean like this?



That causes no problems.
[ August 30, 2006: Message edited by: Rusty Shackleford ]
My pie came with a little toothpic holding up this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 374 times.
Similar Threads
I doubt my garbage collection
HFJ doubt
Objects available for GC..
Regarding reference variable
Interface References?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:51:33.