Gokul Nath KP
Gokul Nath KP

Reputation: 15553

Initializer list - Java

Recently I came across the following definition when preparing for a Java interview:

All executable code executes either within an initializer list or a method.

But from my understanding, Java does not support initializer list as mentioned here

Then why does the author gave a definition like above, when Java does not support initializer list?

Upvotes: 0

Views: 2974

Answers (2)

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

What is should be read as is "Executable code should be within initializer block (static or non static), constructor or a method"

So if System.out.println("executable statement") is our executable statement, then

public class SomeClass{

static{
    System.out.println("executable statement");    
}

{

System.out.println("executable statement");
}

public SomeClass(){

System.out.println("executable statement");
}

public void someMethod(){

System.out.println("executable statement");

}

}

This is valid code while

public class SomeClass{
   System.out.println("executable statement");

}

gives compiler error

Upvotes: 0

kosa
kosa

Reputation: 66637

I can't speak for that Author, but my understanding is, he/she means Static Initialization Blocks (or) instance blocks. Refer this tutorial for more information on these blocks.

Upvotes: 1

Related Questions