• Post Reply Bookmark Topic Watch Topic
  • New Topic

Asking about static variable vs initializer block code  RSS feed

 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I would like to ask you a question about the result of the code below:

The output is: 3

I think that the output should be: 4
Can anyone explain to me?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When will the statement number = 4; be run?
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:When will the statement number = 4; be run?


Do you mean initializer block code is run when class is constructed?
I think that it is run when class is loaded.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think that?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope that is an exam question and not real‑life code. If it is a question, please always tell us where it comes from.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Ta wrote:. . . when class is constructed? . . .
What do you mean by the class being constructed? Constructed is not a term in common use; maybe you meant to say instantiated.
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I hope that is an exam question and not real‑life code. If it is a question, please always tell us where it comes from.


Thanks Mr Campbell Ritchie
I have read the book
[JAVA][OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide Exam 1Z0-808], authors Jeanne Boyarsky and Scott Selikoff

Chapter 01 - Java Building Blocks, pages: 20
I have modified the code from this page to test my idea
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you think that is a static initializer, but is it?
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Maybe you think that is a static initializer, but is it?


No, it is initializer block, not static initializer block
In the code that I've provided is


The code include:
1 static variable number

and 1 initializer block:

in main() method I would like to check the value of variable number by printing it out.
I do not know why the result is: 3
In my opinion, the code should print: 4
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When does a nonstatic initializer execute?
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:When does a nonstatic initializer execute?


When an object of your class is being instantiated.
and static initializer block run when class is loaded to JVM.

This is my confusing part.
Many thanks Mr Knute Snortum!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!