Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StackOverFlowError - Can some one tell me the reason

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am getting Stack overflow error .
Also I was testing for -
I read -
Cloning is potentially dangerous because
If an object being cloned contains a reference variable ,then when the clone is made ,the reference in the clone will refer to the same object as that in the original.
Also if the reference in the clone tries to change the object than it will change the original object .
So Am I going in the right direction ?
 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course. See your class B itself. It is not even reaching clone method.
in main itself is causing stack overflow. (Object inside object problem)
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how I come out of stack overflow ?

I was testing for -
I read -
Cloning is potentially dangerous because
If an object being cloned contains a reference variable ,then when the clone is made ,the reference in the clone will refer to the same object as that in the original.
Also if the reference in the clone tries to change the object than it will change the original object .
CAN you please suggest me another example
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can never create an instance of any class that does this


class B {
B b = new B();
}


because it will give a StackOverflowError. Every instance of B contains another instance of B which contains another instance of B which contains... and it goes on until the stack runs out of room.

This, on the other hand, works fine:


class B {
B b;
}


The variable "b" can refer to another instance of B, but creating an instance of B doesn't automatically create another instance of B, which is what leads to the error.

I can't give a more specific description of how to "fix" the problem, because your program isn't a real program, after all -- we'd have to know what you were trying to accomplish.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for improper presentation , I tried to make it look as a block of Object
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a certain Curiousness , I just tried to declare a object of the class aa static and it worked . (I thought it would belong to the class so no overflow should occur .
Can Someone please give me details on this.!!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have an FAQ called It Doesn't Work Is Useless; now we have "it works", and I still don't understand it

By "it works", do you mean you suffer the overflow, or your application runs without overflow?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic