This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes problem in constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "problem in constructor" Watch "problem in constructor" New topic
Author

problem in constructor

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 908
i m trying a code, and as i think it should prints 10 infinite times, but it is not..
here is the code:

it printing
10 for some lines and than.
"at A.<init><A.java:8>"
"at A.<init><A.java:13>"
number of times.

i don't know why out put is this??
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1295

Hi,

Can you please post complete output? At least first few lines and last few lines?

I suspect that you are getting a OutOfMemoryError due to calling same constructor inside itself.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 908
here is the complete output..


10
10
10
10
10
10
10
10
10
10
<snip: 3499 in total>
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at A.<init>(A.java:7)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
<snip: lines 8 and 13 repeat for 90 lines>
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)10
10
10
10
10
10
10
10
10
10
10
<snip: 124 in total>

at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
<snip: lines 8 and 13 repeat for 898 lines>
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1295

Hi,

Root cause is same as I mentioned - calling a constructor within itself. Hence, program crashed with throwing StackOverflowError as it can be seen in output:

Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.write(Unknown Source)
at java.io.PrintStream.print(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at A.<init>(A.java:7)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
at A.<init>(A.java:8)
at A.<init>(A.java:13)
...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Punit, are you crazy? You posted over 4000 lines of output! I've removed quite a lot of those while still maintaining the relevant information.

Anayonkar is right. When you create a new A(), that creates a new A(10). That in turn creates a new A(), which again creates a new A(10). This continues until a StackOverflowError is thrown.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 908
okay..thank you...
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 908
i am sorry for that.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: problem in constructor
 
Similar Threads
Question from JQPlus
why compile time error???
protected can not be accessed in derived ??
Question on execution of blocks
Could the static keyword cause a bottleneck/ deadlock?