aspose file tools*
The moose likes Beginning Java and the fly likes Object Creation and Construction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Creation and Construction" Watch "Object Creation and Construction" New topic
Author

Object Creation and Construction

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Reason 4 the result of this code?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Can you tell us what is puzzling you ? And UseRealWords.


[My Blog]
All roads lead to JavaRanch
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[Thumbnail for diag.jpg]

swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
can please copy paste that exception whatever you are getting.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ok. Thanks Sir.

Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source)
.......... It's going on....
swapnl patil
Ranch Hand

Joined: Aug 13, 2007
Posts: 80
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

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks a lot Sir!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Creation and Construction
 
Similar Threads
practice question: what is the output?
Object constructor and initialization
array objects?
Restoring an object in finalize() ?
Why not getting Initialized