Forums Register Login

Object Creation and Construction

+Pie Number of slices to send: Send
Reason 4 the result of this code?
+Pie Number of slices to send: Send
Can you tell us what is puzzling you ? And UseRealWords.
+Pie Number of slices to send: Send
This gives a Exception in the line 6. We are creating two objects with the same name, It's confusing. Thanks in advanced. Thank you Sir for your help!
+Pie Number of slices to send: Send
What kind of exception ? StackOverflowError ? Which would not be surprising because you are recursively calling the Test constructor from the Test constructor. It's calling it again and again...
+Pie Number of slices to send: Send
It isn't give the any Type of Exception, instead of it dispaly null , 0 (nul for instance t(Object) and 0 for i) their default value. After that line(line 5), It's give some kind of unknown exception. Here we are creating an Object with the name as t in the main method. It (the Object t) itself has a Object t(instance variable/Field t) which is initialized in the Constructor of the main method Object t.

Could you explain in details?
+Pie Number of slices to send: Send
 

It's give some kind of unknown exception.


There's no unknown exception. A complete stack trace of the exception should be output.

instead of it dispaly null , 0 (nul for instance t(Object) and 0 for i)


Again, you are calling the Test constructor recursively. Take a look at that (ugly) diagram to see what's happening. You're creating new Test instances from the Test constructor. It will loop endlessly.
diag.jpg
[Thumbnail for diag.jpg]
+Pie Number of slices to send: Send
can please copy paste that exception whatever you are getting.
+Pie Number of slices to send: Send
Ok. Thanks Sir.

Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source)
.......... It's going on....
+Pie Number of slices to send: Send
 

Christophe Verré wrote:What kind of exception ? StackOverflowError ? Which would not be surprising because you are recursively calling the Test constructor from the Test constructor. It's calling it again and again...



he is right then. You are call the constructor recursively that's why your program is throwing this exception.
remove t = new Test(); from constructor & your code will work without any error

+Pie Number of slices to send: Send
Thanks a lot Sir!!!
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1103 times.
Similar Threads
Why not getting Initialized
Object constructor and initialization
Restoring an object in finalize() ?
practice question: what is the output?
array objects?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:46:08.