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)
Ernest Friedman-Hill
,
author and iconoclast
staff
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.
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?
Ernest Friedman-Hill
,
author and iconoclast
staff
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.
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 ]
Post by:autobot
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