Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static initialization in interface

When I tried to write something like this:

public interface MyInterface {     static {         System.out.println("Hello!");     } } 

the compiler could not compile it.

But when I wrote something like this:

interface MyInterface {     Integer iconst = Integer.valueOf(1); } 

and decompiled it, I saw static initialization:

public interface MyInterface{     public static final java.lang.Integer i;      static {};       Code:       0:   iconst_1       1:   invokestatic    #1; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;       4:   putstatic       #2; //Field i:Ljava/lang/Integer;       7:   return } 

Could you please explain this behavior to me?

like image 969
Sergey Morozov Avatar asked Nov 01 '13 07:11

Sergey Morozov


People also ask

Can we initialize static variable in interface?

You can have static initialisation, but you cannot have a static block. The fact the static initialisation needs a static code block to implement does change the Java syntax. The point is you are not meant to have code in an interface (before Java 8) but you are allowed to initialise fields.

What is static initialization?

A Static Initialization Block in Java is a block that runs before the main( ) method in Java. Java does not care if this block is written after the main( ) method or before the main( ) method, it will be executed before the main method( ) regardless.

What is static initialization give an example?

Example. Live Demo public class Demo { static int[] numArray = new int[10]; static { System. println("Running static initialization block."); for (int i = 0; i < numArray. length; i++) { numArray[i] = (int) (100.0 * Math. random()); } } void printArray() { System.

Is static block allowed in interface?

Interfaces do not allow static blocks but do allow public final static member. If someone wants to initialize the variable based on some logic, it is not possible in interfaces but possible in classes.


2 Answers

Interfaces should not have side-effects and that even applies to static intializers. They would have highly JVM-implementation dependent behavior. Look at the following code

public class InterfaceSideEffects {   public static void main(String[] args) {     System.out.println("InterfaceSideEffects.main()");     Impl i=new Impl();     System.out.println("Impl initialized");     i.bla();     System.out.println("Impl instance method invoked");     Foo f=new Impl();     System.out.println("Impl initialized and assigned to Foo");     f.bla();     System.out.println("Foo interface method invoked");   } } interface Foo {   int dummy=Bar.haveSideEffect();   void bla(); } class Bar {   static int haveSideEffect() {     System.out.println("interface Foo initialized");     return 0;   } } class Impl implements Foo {   public void bla() {   } } 

What do you think, when will interface Foo initialized be printed? Try to guess and run code afterwards. The answer might surprise you.

like image 156
Holger Avatar answered Oct 02 '22 14:10

Holger


You can have static initialisation, but you cannot have a static block. The fact the static initialisation needs a static code block to implement does change the Java syntax.

The point is you are not meant to have code in an interface (before Java 8) but you are allowed to initialise fields.

BTW you can have a nested class or enum which has as much code as you like and you can call this while initialising a field. ;)

like image 32
Peter Lawrey Avatar answered Oct 02 '22 14:10

Peter Lawrey