• Post Reply Bookmark Topic Watch Topic
  • New Topic

The Static Blocks in Java Blues  RSS feed

 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must have missed something along the way. The static block should execute second. Why isn't int x set to 5? The output this code produces is 3 meaning the static variables were initialized to the default value of zero as the code executed. It's as if the static block never set the value of x to 5!!! What am I missing?

Thanks in advance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That declares a separate, independent x variable whose scope is just that static block. It should be simply x = 5;
 
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
Ivan Turner wrote:I must have missed something along the way. The static block should execute second. Why isn't int x set to 5? The output this code produces is 3 meaning the static variables were initialized to the default value of zero as the code executed. It's as if the static block never set the value of x to 5!!! What am I missing?

Thanks in advance.


The local x variable in the static block is being set to 5. Of course, this local x variable, and the static variable (also an x variable) are not the same variable.

[EDIT: Wow. Got beaten to the answer by 15 seconds !! ]

Henry
 
Ivan Turner
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, It sounds like int variable x in the static block has a scope only within the static block. I was under the impression that the purpose of the static block was to set static class variables. Thanks for clearing that up. Removing the "int" from the static block's x variable made it work as I had expected by setting the class variable to 5. Thanks a million!!!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Turner wrote:Thanks, It sounds like int variable x in the static block has a scope only within the static block. I was under the impression that the purpose of the static block was to set static class variables.
I

It is. But your version was setting a locally scoped variable. By removing the type declaration inside the block, you turn that x into the static variable, and then you can initialize it.

Note, however, that for something that simple, you can just do


and skip the static block altogether. There are only two cases that you really need static blocks:

1) When you need to initialize something that's more complex than a single declaration+initialization line can accomplish.

2) When you need to do something that can throw a checked exception, and you need to wrap that exception in an ExceptionInInitializerError.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!