1

I'm having some confusion with the reasoning behind what seems to me to be an inconsistency.

For example

public class Test
{
    static int a;
    public static void main(String[] args)
    {
        System.out.println(a);
    }
}

So that will print out 0, as expected. But say we had this instead,

public class Test
{

    public static void main(String[] args)
    {
        int a;
        System.out.println(a);
    }
}

This won't compile for me, complaining that a hasn't been initialized. I was expecting it to print out 0...

Which leads me to some questions:

1) Why don't function scoped variables have default values?

2) Could the static keyword be the reason? And why?

3 Answers3

4

The Java Language Specification explains the default Initial values of Variables

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):

For type byte, the default value is zero, that is, the value of (byte)0.

For type short, the default value is zero, that is, the value of (short)0.

For type int, the default value is zero, that is, 0.

For type long, the default value is zero, that is, 0L.

For type float, the default value is positive zero, that is, 0.0f.

For type double, the default value is positive zero, that is, 0.0d.

For type char, the default value is the null character, that is, '\u0000'.

For type boolean, the default value is false.

For all reference types (§4.3), the default value is null.

and also states

A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16).

Both your questions can be answered by "Because the JLS says so".

A more complete answer would be the following:

A Class is a description of state and behavior. An object is the actual data. If you create an object, it must have definitive state, it cannot be in an uninitialized state.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Ah! I had read through the first part, but I didn't read the 14.4 and related docs. /embarrassed. Thanks!! EDIT: Yea, I was hoping there was a more meaningful answer besides the 'it says so response', but I'm hearing this is more of the case in Java, (Python programmer here). –  Sep 27 '13 at 01:10
0

1) Why don't function scoped variables have default values?

It is the rule defined by JLS that method variables are not initialized to thier default values. You need to initialize them beforeusing

2) Could the static keyword be the reason? And why?

Again form JLS rules, class instance variables are initialized to default values by the compiler. You may or may not initialize them.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Java compiler never assigns default values to Local variables as mentioned in the link http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

You have to explicitly initialize them.

Arun
  • 321
  • 1
  • 5