• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about the running order of static{} block

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the following two snippets output 3 and 5, respectively?

output:3


output:5
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:Why the following two snippets output 3 and 5, respectively?


Can you tell us what you expected the results to be? And why?

Henry
 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
nemo zou wrote:Why the following two snippets output 3 and 5, respectively?


Can you tell us what you expected the results to be? And why?

Henry


I thought the first one is a compilation error since it defines two int a in the same class. The second output is 3 because static{} always runs first in a class...
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:I thought the first one is a compilation error since it defines two int a in the same class.


One is a static variable. One is a local variable. And of course, it is possible to have a local variable with the same name as a static variable, which hides the static variable while it is in scope.

nemo zou wrote:The second output is 3 because static{} always runs first in a class...


Static variables (along with their initialization) and static initializers, are executed in the order they are encountered in source code.

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
nemo zou wrote:I thought the first one is a compilation error since it defines two int a in the same class.


One is a static variable. One is a local variable. And of course, it is possible to have a local variable with the same name as a static variable, which hides the static variable while it is in scope.

And just for completeness: a local variable can also hide an instance variable. Although this code has plenty of variables with the same name, this code compilesAnd it's up to you to work out what the above code will print

You can find a nice overview of initializing (final) variables (static, instance and local) and its consequences in this great post. (Disclaimer: it's one of my own posts )

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nemo zou wrote:The second output is 3 because static{} always runs first in a class...

The order of execution is (of course) very important. And there are some rules you need to know. Certainly when you are mixing static initializer blocks, initializer blocks, constructors and different classes together. Luckily you'll find a detailed explanation with a few code snippets in this thread.

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Henry Wong wrote:
nemo zou wrote:I thought the first one is a compilation error since it defines two int a in the same class.


One is a static variable. One is a local variable. And of course, it is possible to have a local variable with the same name as a static variable, which hides the static variable while it is in scope.

And just for completeness: a local variable can also hide an instance variable.

And I forgot to mention that you can't have a class/static variable and an instance variable with the same name. So the following code will not compileAnd the same applies to class/static method and an instance method as well. This code also does not compile

Hope it helps!
Kind regards,
Roel
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel, Great examples. Thank you !

P.S I mentally ran the code to get a wrong output , that's because i called method2() first and then executed method1(). I think i will simply blame the conventions used here... : )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic