Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is initalization of string in java necessary...?

Tags:

java

class TestMe{   
    public static void main (String args[]){
        String s3;  
        System.out.print(s3);
    }
}

why the compiler is giving a error,refernce object has a default value of null,why it is not the output...??

error: variable s3 might not have been initialized

like image 848
Shivam Dhabhai Avatar asked Feb 18 '23 19:02

Shivam Dhabhai


1 Answers

It's an error because the JLS says so in §14.4.2. Execution of Local Variable Declarations:

If a declarator does not have an initialization expression, then every reference to the variable must be preceded by execution of an assignment to the variable, or a compile-time error occurs by the rules of §16.

like image 137
NPE Avatar answered Feb 20 '23 08:02

NPE