Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Blocks - when are they executed

Tags:

java

I'm totally new to Java and I'm wondering why my static block is not executing.

public class Main {

public static void main(String[] args) { 
    Account firstAccount = new Account();  
    firstAccount.balance = 100;
    Account.global = 200;  
    System.out.println("My Balance is : " + firstAccount.balance);
    System.out.println("Global data   : " + Account.global);

    System.out.println("*******************");
    Account secondAccount = new Account();
    System.out.println("Second account balance  :" + secondAccount.balance);
    System.out.println("Second account global   :" + Account.global);

    Account.global=300;
    System.out.println("Second account global   :" + Account.global);

    Account.add();  }
}

public class Account 
{
int balance;        
static int global;  

void display()   
{
System.out.println("Balance     : " + balance);
System.out.println("Global data : " + global);
}

static   // static block
{
    System.out.println("Good Morning Michelle");

}
static void add()  
 {
    System.out.println("Result of 2 + 3 " + (2+3));
    System.out.println("Result of 2+3/4 " + (2+3/4));  
}
public Account() {
    super();
    System.out.println("Constructor");

}
}

My output is:

Good Morning Michelle
Constructor
My Balance is : 100
Global data   : 200
*******************
Constructor
Second account balance  :0
Second account global   :200
Second account global   :300
Result of 2 + 3 5
Result of 2+3/4 2

I want to know why "Good Morning Michelle" was not displayed when I went in with the second account.

From the research I have done, this static block should execute each time the class is called (new Account).

Sorry for the real beginner question. Michelle

like image 946
Michelle Avatar asked May 01 '13 19:05

Michelle


People also ask

Is static block executed before Main?

In Java static block is used to initialize the static data members. Important point to note is that static block is executed before the main method at the time of class loading.

When static method is executed?

It executes whenever the class is loaded in memory. One class can have numerous static blocks, which will be executed in the same sequence in which they are written.

When and where static blocks are executed in Java?

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.

Is static block executed before constructor?

Order of execution When you have all the three in one class, the static blocks are executed first, followed by constructors and then the instance methods.


1 Answers

Your static block that prints "Good Morning Michelle" is a static initializer. They are run only once per class, when that class is first referenced. Creating a second instance of the class will not cause it to run again.

like image 181
rgettman Avatar answered Sep 16 '22 12:09

rgettman