0

I am new to Java and came across the Static Block. As far as I understood, all the static statements (static initialization, static blocks etc) are run together before the code is executed.

My doubt is whether this happens during run time or compile time?

During compiling, the code is converted to machine language and at run time this machine language code is executed. So, is the order of execution changed during compiling and all the static statements put together at the start of the code so that they get executed before anything else??

Sorry, if this looks like a very simple question..

Engineer
  • 1,436
  • 3
  • 18
  • 33
Nishit
  • 1,276
  • 2
  • 11
  • 25

2 Answers2

0

They are loaded at runtime.

Static means: that the variable belong to the class, and not to instances of the class. So there is only one value of each static variable, and not n values if you have n instances of the class.

Keval Trivedi
  • 1,290
  • 2
  • 13
  • 29
0

Static initialization block is being ran when JVM (class loader) loads StaticClass (when it is first time referenced in code).

For more details about refer static block in java

rachana
  • 3,344
  • 7
  • 30
  • 49