0

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?

Community
  • 1
  • 1
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126

2 Answers2

1

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.

kosa
  • 65,990
  • 13
  • 130
  • 167
0

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

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56