Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Creation and Construction

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reason 4 the result of this code?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us what is puzzling you ? And UseRealWords.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can please copy paste that exception whatever you are getting.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Sir!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic