aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes [n] Initialization of object member before object creation? - or multiple threads? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "[n] Initialization of object member before object creation? - or multiple threads?" Watch "[n] Initialization of object member before object creation? - or multiple threads?" New topic
Author

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

Nirav Patel
Greenhorn

Joined: May 27, 2005
Posts: 22
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?


----<br />Nirav Patel<br />Software is the bottleneck in the digital world!
Nirav Patel
Greenhorn

Joined: May 27, 2005
Posts: 22
Hi group!
busy?

bump!
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
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


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
shyam ramineni
Ranch Hand

Joined: Jul 08, 2005
Posts: 43
Hi,

Can we do static import in java1.4.

Thanks
Shyam Ramineni


<a href="http://hyderabaditeducation.com" target="_blank" rel="nofollow">http://hyderabaditeducation.com</a>
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Can we do static import in java1.4.

Nope.
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Right,So the rule is
All initializations and calls to blocks (instance or static initializer blocks get executed and then the constructor get executed).


/** Code speaks louder than words */
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
 
subject: [n] Initialization of object member before object creation? - or multiple threads?