Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can i know how output came -1930?  RSS feed

 
srikanth nedamanuri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dave Tolls
Rancher
Posts: 2914
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code please use code tags so it retains its formatting.

This case is down to the order the static code is run.

All the static code is executed before main() starts (when the Test3 class is loaded).
It is executed in the order it appears in the class.

This should let you figure out why year is zero when the constructor runs.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:When posting code please use code tags so it retains its formatting.


Agreed. And since the ranch is currently a bit restrictive with editing at the moment, let me fix it for the OP.


And Srikanth... welcome to the ranch.

Henry
 
srikanth nedamanuri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thankyou for your reply


when below variable load in to memory before constructor calling or  after constructor calling??
public static final int year = Calendar.getInstance().get(Calendar.YEAR);





 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to know that in the first place?
If you go through the Java® Language Specification (=JLS) (try §12.2), you will find that static members are initialised when the class is loaded. Whether the Calendar object is loaded into memory or whether escape analysis allows it to be created on the stack is an implementation detail. Since you didn't assign the Calendar object to a variable or anything, it becomes eligible for GC immediately and might only have a very temporary existence. This will probably vary from implementation to implementation.
Beware: The JLS can be very difficult to read.

And welcome again
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth nedamanuri wrote:
when below variable load in to memory before constructor calling or  after constructor calling??
public static final int year = Calendar.getInstance().get(Calendar.YEAR);


Well, initialization of static variables are done right after the class is loaded. And initialization of instance variables (along with constructor, etc.) are done when an instance of the class is created.... and since a class must be loaded before an instance is created, then initialization of static resources *should* happen before initialization of instance resources.

Of course, there are always exception cases....

In this case, you are creating an instance of the class, during the static initialization of the class. This means, that an instance will have to be created, before static initialization can compete.

Henry
 
Dave Tolls
Rancher
Posts: 2914
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth nedamanuri wrote:
when below variable load in to memory before constructor calling or  after constructor calling??
public static final int year = Calendar.getInstance().get(Calendar.YEAR);


When the class is loaded all static variables are loaded with default values (null, false or 0).
There is then a pass through the class executing the static lines.

So, for the following example, bar is 0 at the point the constructor is executed.

This is what's happening to your year variable.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . you are creating an instance of the class, during the static initialization of the class. . . .
I hadn't noticed that bit. Of course, the instance field is thereby set to its default value after the static field is initialised. No, that bit is wrong. The value of the static field will still have its default value (=0) when the − 1930 bit is executed.

That is why you shouldn't write that sort of code. Keep things simple and you will understand what is happening.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!