aspose file tools*
The moose likes Java in General and the fly likes StackOverFlowError - Can some one tell me the reason Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "StackOverFlowError - Can some one tell me the reason" Watch "StackOverFlowError - Can some one tell me the reason" New topic
Author

StackOverFlowError - Can some one tell me the reason

Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121


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 ?


Understanding is Everything - Peter Lord
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
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

Joined: Jul 29, 2010
Posts: 121

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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121


Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Sorry for improper presentation , I tried to make it look as a block of Object
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

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

Joined: Oct 13, 2005
Posts: 38765
    
  23
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StackOverFlowError - Can some one tell me the reason