• Post Reply Bookmark Topic Watch Topic
  • New Topic

The order of initialization  RSS feed

 
Jerry Ye
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote my code like this

And I found that the time of time2 is earlier than time1. How is this possible? Is it because time2 is initialized first as there is an initializer block?
The Java Tutorial explains that "The Java compiler copies initializer blocks into every constructor". But shouldn't initialize field run first?
Anyone can explain it?
Thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jerry Ye wrote:
The Java Tutorial explains that "The Java compiler copies initializer blocks into every constructor". But shouldn't initialize field run first?
Anyone can explain it?


Instance initializers, and initialization of instance variables, are executed in the order that they are encountered in source code. And in this case, the instance initializer is before the initialization of the instance variable.

Henry
 
Jerry Ye
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Instance initializers, and initialization of instance variables, are executed in the order that they are encountered in source code. And in this case, the instance initializer is before the initialization of the instance variable.

Isn't time1 intialized when it is declared? So the declaration and initialization are seperated?
If we can put initializer before the declaration, why can't we refer to it? What does it mean "not defined"?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code won't compile.  You're trying to use time2 before it's declared.
 
Jerry Ye
Greenhorn
Posts: 25
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:The code won't compile.  You're trying to use time2 before it's declared.

You mean the one I gave at first? Why not have a try?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's have this example:


It would compile fine.but generally knute is correct that you cannot use a variable before it is declared.but their is an exception mentioned in Java® Language Specification(JLS-§8.3.2.3).
here it is:
JLS wrote:The declaration of a member needs to appear textually before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
.The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
.The usage is not on the left hand side of an assignment.
.The usage is via a simple name.
.C is the innermost class or interface enclosing the usage.

For a detailed understanding here is the linkJLS- §8.3.2.3.

Hope it helps!

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though,on my machine i am not getting any difference in time.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:Though,on my machine i am not getting any difference in time.
You are, but the difference might be less than the temporal resolution of the objects. It says here that the temporal resolution of LocalTime is in nanoseconds, so how are you printing the times?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!