• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we have multiple static blocks?

 
rakesh repala
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we have multiple static blocks in one class?how it will be exceution statrs?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi rakesh,

welcome to the ranch

You can have multiple static initializer blocks in Java and they are executed in the order they appear in your source code.

Marco
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch

In short, yes there can be multiple static blocks, and they are executed from top to bottom as they appear in the code.
This article seems to give a nice explanation on static blocks.
 
rakesh repala
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks...
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rakesh

Here at the javaranch we try to learn not teach
if only you had put multiple static blocks in your code and checked the result,you would not have had this doubt..
we feel the best way to learn is to do a lot of coding.Please feel free to ask and share your knowledge
Happy ranching
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jitendra Jha:
we feel the best way to learn is to do a lot of coding.

I agree. Whenever you want to ask "will this compile" or "is this possible", the best thing to do is simply test it out. The compiler and JVM have better knowledge of Java than any of us do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic