• 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

StackOverFlowError - Can some one tell me the reason

 
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.!!!
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
There's a way to do it better - find it. -Edison. A better tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic