• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reference variable

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic