user3851283
user3851283

Reputation: 347

Does a static function get called in a class automatically whenever I invoke a function?

So I have a public class and within this class are several public functions including a static method.

public class TestVocabValidator {

    static { getEnumList( vocabList.values() ); }

    public static Iterator<String> getVocabEntries(String x) {
        return null;
    }

}

Whenever I call on the function getVocabEntries(), does the static method get called automatically?

Upvotes: 0

Views: 715

Answers (2)

Mena
Mena

Reputation: 48404

The static blocks (e.g. static {...}) are executed once, when the class' name is referenced and the class is loaded.

The static methods (e.g. getVocabEntries) are executed every time they are invoked.

Upvotes: 3

Anubian Noob
Anubian Noob

Reputation: 13596

Not whenever you call that static method. The first time that class is loaded, in this case the first time you call that method, the static block is called.

Upvotes: 1

Related Questions