4
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

doniyor
  • 36,596
  • 57
  • 175
  • 260
Shivam Dhabhai
  • 331
  • 3
  • 13
  • i changed the class name, otherwise it can come to problems if your class is called ``Java`` – doniyor Jan 20 '13 at 09:09
  • possible duplicate of [Uninitialized class members in Java do not issue any compiler errors. local variables however do. Why?](http://stackoverflow.com/questions/8521917/uninitialized-class-members-in-java-do-not-issue-any-compiler-errors-local-vari) – Lion Jan 20 '13 at 09:19

6 Answers6

5

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

local variables should be initialized before using them, local vars dont get default values in java, thus your string s3 doesn't get default value null as it is a local variable , thus the compiler error.

Fom JLS:

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.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
1

The default value of null only applies to non-final fields of a class.

All other cases require initialisation before first use

Stephen Connolly
  • 13,872
  • 6
  • 41
  • 63
  • Or array's cells also have their value initialized to default values even if the array is local. – Mik378 Jan 20 '13 at 09:18
  • The array cells are not fields. An uninitialised array is eg `Object[] foo;` if you allocate the array with, eg `new Object[50]` the property of `new` is that it sets everything to `0` or `null` depending on the type. – Stephen Connolly Jan 20 '13 at 09:23
  • Yes justly, each cell is initialized with the array's type default value. I didn't only talk about fields or non-initialized array, but rather the cases where default values applied. For instance, a boolean array would have all its cells initializes to `false`. – Mik378 Jan 20 '13 at 09:32
  • The question was about fields specifically, as was my answer ;-) – Stephen Connolly Jan 20 '13 at 09:42
0

Yes, it's necessary.

String s3;
s3 = "Something....";
System.out.print(s3); // prints "Something..."

Before you use a local variable, you have to initialize it.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
0

what i know about local variables is:
Local variables are declared mostly to do some calculation.So it is the programmer's decision to give the value to the variable and it should not take default value. If programmer by mistake did not initialize a local variable, then it takes default value, then the output goes wrong. so local variables will ask programmer to initialize before he uses the variable to avoid making mistake.

doniyor
  • 36,596
  • 57
  • 175
  • 260
0

The unique scenarios where defaut values are used are the cases where the concerned variables are object's fields or array's components even local. Indeed, arrays ALWAYS initializes their cells with their appropriate default values.

Thus, in your case, your variable doesn't come from a field ( since local to the method) and hasn't participated with an array initialization. So compiler logically complains ..

Mik378
  • 21,881
  • 15
  • 82
  • 180