1

I know the static blocks run before main. But do the normal blocks also run before main or after main ?

class prog
{
static
{
System.out.println("Static");
}
{
System.out.println("Block");
}

public static void main(String args[])
{
prog obj=new prog();
System.out.println("Main");
}
}

6 Answers6

2

Actually the static block or SIB (Static initialization Block ),loads at the time of class loading, so it executes before main()..

But same is not the case with Non-static block or IIB (Instance Initialization Block ), It is loaded only at the time of Object Creation(Instance), as the name also clarifies the same...

I think you got my Point...

1

Static block are executed when classes are loaded where as normal blocks are executed when an instance of the class enclosing the block is created.

Just for the record prior to java 7 static blocks were executed before main() method was searched in the project. But from java 7 main() is first looked up. So you will get an error if you don't have main.So saying I know the static blocks run before main is a bit ambiguous. main() method is looked up prior to executing static blocks but the main execution will start after the static blocks are handled.

Also non static blocks are executed before the corresponding constructor is invoked. For example

public class Tester {

{
    System.out.println("In some random block");
}

static {
    System.out.println("In static block");
}

public Tester() {
    System.out.println("Constructor");
}


public static void main(String args[]) {
    Tester t = new Tester();
}


}

will print

In static block
In some random block
Constructor
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0

The non static block one runs whenever you instantiate an object, in your case inside main. try printing something before prog obj=new prog();

if i have something like the below, it will run before main.

class prog {
    static {
        System.out.println("Static");
    }
    {
        System.out.println("Block");
    }

    prog obj = new prog();
    public static void main(String args[]) {
        System.out.println("Main");
    }
}
Claudiu
  • 1,469
  • 13
  • 21
  • 1
    *The non static block one runs whenever you instantiate an object* of the class that contains these blocks. – Luiggi Mendoza Sep 25 '13 at 05:13
  • Also, it is worth mentioning that the *non-`static`* blocks of code in the class will be executed **before** executing the class constructor. See http://stackoverflow.com/a/17001104/1065197 – Luiggi Mendoza Sep 25 '13 at 05:18
  • Yes, the non-static blocks are part of the `init` method generated by the compiler for all classes and that method is run before constructor. – Claudiu Sep 25 '13 at 05:20
0

Static block is block in which if we specify any variable or class static we can access its data members into another class or outside that class through its class name. Normal Block is a block in which a class is created or object of that class created.If we want to use it in another class we have to create the constructor of that class and then we can access its object. So this is main difference in static block and normal Block

0

Normal block will be run when the instance of that class will be created at compile time. and static block will be run only once in the execution of program . so if you want some thing that should print before the instance of that class created then put it in the static block.

Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
0

Normal init blocks are run when an instance of a class is instantiated. Static methods, like main, doe not need an instance, so these two things are not related.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275