0

In a Java class at what point does a static loop get called? and how often is it referenced? im only familiar with calling a method, and

I have seen it on this truly excellent answer to Euler Q14, been doing a java a year but this the first time ive seen this here:

stackoverflowquestion

static {
for (int i = 10; i < cache.length; i *= 2)
    collatzNum(i - 1);
        }
Community
  • 1
  • 1
Fionán
  • 111
  • 10

2 Answers2

2

This is called static initialization block. You can used it to initialize static variables. This block will be called when the class is loaded.

FYI

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code

For more details, check here

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

The static blocks are called in the sequence in which they are declared, and they are get called when the class is loaded by your jvm ---ClassLoader

Girish
  • 1,717
  • 1
  • 18
  • 30