• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[n] Initialization of object member before object creation? - or multiple threads?

 
Nirav Patel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am confused about the output of the following code:

Output is:
In getValue
In cotr
i = 4, j = 4

Why the constructor statement is printed after the method call? How the method is called even before creating the object? (it's not the static method)
Any idea?
 
Nirav Patel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi group!
busy?

bump!
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When an object is created, all the object's fields are initialized to zero or null before the ctor executes. Then any field with an explicit initialization assignment is assigned the provided value. Only then does then ctor execute.

In your case, the initialization value is the return value from a method call. That's fine.

BTW, a class can contain non-static initialization code blocks. These look just like static blocks, but the "static" keyword is absent. This code executes at the same time as field initialization.

Hope this helps.
-- Phil
 
shyam ramineni
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can we do static import in java1.4.

Thanks
Shyam Ramineni
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we do static import in java1.4.

Nope.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right,So the rule is
All initializations and calls to blocks (instance or static initializer blocks get executed and then the constructor get executed).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Kitty Creation", you have been asked here to change your displayed name. I have pointed it out just in case you have missed it. The profiles of posters who use obviously false names will be disabled.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic