aspose file tools*
The moose likes Beginning Java and the fly likes problem in constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in constructor" Watch "problem in constructor" New topic
Author

problem in constructor

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 939
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: 1466
    
    5

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, OCEEJBD)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 939
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: 1466
    
    5

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: 19556
    
  16

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Punit Jain
Ranch Hand

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

Joined: Aug 20, 2011
Posts: 939
i am sorry for that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in constructor
 
Similar Threads
Question on execution of blocks
protected can not be accessed in derived ??
why compile time error???
Could the static keyword cause a bottleneck/ deadlock?
Question from JQPlus