Search code examples
javastaticfinal

Why the below code snippet is not giving compile time error?


After declaring a static final instance varibale, one ArrayList object is being assigned to it. But when I am adding one extra semi-colon, it is fine with the java compiler. Here is the code.

import java.util.ArrayList;
import java.util.List;

public class FinalExample {

    private static final List<String> foo = new ArrayList();; //double semicolons are fine with compiler

    public static void main(String[] args){
        System.out.println(FinalExample.foo);//Result is - []
    }

Solution

  • A semicolon ends a statement. So when you give multiple semicolons in java the compiler thinks the second semicolon as en empty statement and does not complains for it.