Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reference variable  RSS feed

 
Arun Prasath
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • 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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!