-1

I Have doubts about how it work static blocks in Java. As I understood, code inside static block is executed everytime that the class is loaded by JVM. What I don't understand is WHEN exactly a class is loaded.

For Example, let's suppose I have static Block inside the class MyObject, and then from another class I have this code:

MyObject o1= new MyObject();
MyObject o2= new MyObject();

How many times the static block is executed in this case?

Szanownego
  • 239
  • 1
  • 11
  • What is the question ? When class loaded or how many times static block executed ? – seal Oct 10 '16 at 11:40
  • @seal - I asked both questions – Szanownego Oct 10 '16 at 11:44
  • 1
    A class is only loaded once, the first time it needs to be loaded. Exactly when the JVM does this depends on the JVM implementation. – Peter Lawrey Oct 10 '16 at 11:47
  • @PeterLawrey - .... except if you have tree of classloaders. – Stephen C Oct 10 '16 at 12:17
  • @StephenC yes, but the JVM doesn't consider those classes to be `==` even if they have the same name and code. ;) You could consider them to be the same class conceptually. – Peter Lawrey Oct 10 '16 at 12:20
  • 1
    Hmmm. It depends on how you "hold your mouth". Another way of saying that is that the class is loaded multiple times, and each time it is loaded you get a different type (and a different `Class` object). – Stephen C Oct 10 '16 at 12:33
  • The JLS says this *"When two reference types are the same, they are **sometimes** said to be the same class or the same interface."* Emphasis added. It also says that the phrases *"runtime class"* or *"runtime interface"* are sometimes used. – Stephen C Oct 10 '16 at 12:40

1 Answers1

1

Code inside static block is executed when the class is loaded by JVM first time.

If you load again then the static block will not execute. You can explicitly load the class by using Class.forName("class") method to execute your static block.

in your case,

MyObject o1= new MyObject();
MyObject o2= new MyObject();

When you first create object, your static block will execute. Again you create another object and it will not execute. They share same copy of data.

Pirate
  • 2,886
  • 4
  • 24
  • 42
  • 1
    *"everytime that the class is loaded by JVM first time"* - And how many times is that? :-) – Stephen C Oct 10 '16 at 12:15
  • only for first time. – Pirate Oct 10 '16 at 12:36
  • What is “*everytime* that the class is loaded by JVM *first time*”? How often can a class be loaded the “*first time*”? And what do you mean with “*If you load again*”; what do you think, how often can you load a class? – Holger Oct 10 '16 at 12:38
  • that means if you want to reload your class. – Pirate Oct 10 '16 at 12:41
  • But that isn't the first time. That is the second time, or the third time. The only way you can observe something happen the first time multiple times is if you inhabit a multiple universes :-) – Stephen C Oct 10 '16 at 12:43
  • exactly, if you load create object of your class first time, static block will execute. Again if you create object, it will not execute your static block. But in case you again want to run your static block, load your class using Class.forName. is that clear? – Pirate Oct 10 '16 at 12:45
  • 1
    No. It is not clear. Something happening "the first time" more than once is a logical contradiction. A logical contradiction is never clear. – Stephen C Oct 10 '16 at 12:48
  • That is not happening. You are make them to happen or run according to your need. – Pirate Oct 10 '16 at 12:49
  • That’s much better. To improve clarity, instead of “If you load again” you should say something like “If you *try* to load again” as subsequent load request do not actually load the class but return the already loaded class. – Holger Oct 11 '16 at 13:59
  • yeah, Thanks @Holger to correct me. – Pirate Oct 12 '16 at 07:53