124697
124697

Reputation: 21893

What does this 'static' mean and why is it like that

public class tt {
static{
    System.out.println("class tt");
    }
}

It the first time ive come across it and im wondering what it is and what it's used for

Upvotes: 3

Views: 267

Answers (5)

OrangeDog
OrangeDog

Reputation: 38777

It is the static initialiser of the class. When the class is loaded, the static initialiser is run. It is like the constructor, but for the class rather than for individual objects.

Multiple static initialisers can appear in a class, as well as direct initialisers for static variables. These will be combined into one initialiser in the order in which they are declared. For example, the following will print "foo" to stdout whenever the class is loaded (usually once per application).

public class Foo {

  static String a;

  static {
    a = "foo";
  }

  static String b = a;

  static {
    System.println(b);
  }

}

Upvotes: 5

user467871
user467871

Reputation:

here is the static initializer tutorial http://download.oracle.com/javase/tutorial/java/javaOO/initial.html

It runs when the class is loaded before the initialization.

public class A {

    static {
        System.out.println("A from static initializer"); // first 
    }

    public A(){
        System.out.println("A"); // second
    }

    public static void main(String[] args){
        new A();
    }
}

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240890

Its initilizer block

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example:

static {
    // whatever code is needed for initialization goes here
}

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. There is an alternative to static blocks —you can write a private static method:

class Whatever {
    public static varType myVar = initializeClassVariable();

    private static varType initializeClassVariable() {

        //initialization code goes here
    }
}

The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.

Upvotes: 4

Michael Borgwardt
Michael Borgwardt

Reputation: 346270

It's a static initializer block. It will be executed once when the class is first loaded, along with static field initializers like this:

private static int staticField = someMethod();

The difference is that an initializer block can contain control flow structures like try/catch blocks.

Upvotes: 0

Karl Knechtel
Karl Knechtel

Reputation: 61509

It is a static initializer. The code inside that block runs when the JVM loads the class, which is immediately before the first time the program needs to do anything with that class (e.g. look up a static field, call a static method, instantiate an object,...).

Upvotes: 1

Related Questions