1

I am beginner in java and would like to clear an idea about variable initialization. Here is the code --

public class NumberSequence {
  private static int n;
  public static void main(String[] args) {
    print(myTest());
    print(myTest());
  }

  private static int myTest() {
    n++;
    return n * n;
  }
}

with two successive calls of the method myTest() the outputs are 1 and 4. So I understand that the default value of n is 0. So for the first call of myTest(), n would be incremented by one resulting 1 and then the multiply line will be executed, that is, 1 * 1 which is 1.

For the next call of myTest(), n will be initially 1, then it will be incremented to 2, then the multiplication, that is 2 * 2 which is 4

Now I am trying to do this --

public class NumberSequence {
  public static void main(String[] args) {
    int m;
    m++;

    int n = m * m;
    print("n = " + n);
  }
}

Here it gives complier error that m is not initialized which is obvious. So my question is -- how come compiler doesn't complain in case of the first option where I created myTest() method. There I do not initialize the value of n. Is it the case that compiler is doing the initialization? I would like to get this clarification.

Akg
  • 259
  • 1
  • 3
  • 15
  • See http://stackoverflow.com/questions/1831290/static-variable-initialization – zakinster Mar 11 '14 at 12:45
  • You may also want to look at [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) – sanbhat Mar 11 '14 at 12:53
  • I guess this question has already been asked: http://stackoverflow.com/questions/8704423/when-static-variables-are-initialized-in-java – attaboy182 Mar 11 '14 at 12:55

4 Answers4

3

All class or instance fields are initialized to 0, 0.0, null etc. by default. All local variables aren't initialized by default at all.

public class NumberSequence {
  // Static field is initailized to 0 by default
  private static int n; 

  public static void main(String[] args) {
    // Quite OK: n being a static field is initialized to zero
    n = n * n;

    // local variable, aren't initialized 
    // (so it contains trash)
    int m;
    // Attempt to use trash value (since m hasn't been initialized so far) 
    // results in compiler warning/error 
    int n = m * m;
Dmitry Bychenko
  • 149,892
  • 16
  • 136
  • 186
2

Is it the case that compiler is doing the initialization?

n is a static filed of NumberSequence class, which will be initialized with zero by default, if you don't initialized. But, m is a method local variable, you need to explicitly initialized it.

Abimaran Kugathasan
  • 26,826
  • 11
  • 67
  • 101
1

Since the compiler cannot check if static variables are initialized at compilation time, it gives initial values for them.

On the other hand, local variables should and must be initialized before they are used and the compiler can check this for you, and will arise an error if they're not.

Note that the variable is initialized because it's a class member, thus it's getting its default value, which is 0 for ints.

Maroun
  • 87,488
  • 26
  • 172
  • 226
  • It has nothing to do with `static`. Any field, `static` or a plain non-`static` instance field is initialized with the default value of the type of variable. – Erwin Bolwidt Mar 11 '14 at 13:47
  • @ErwinBolwidt I **was not** referring instance field. See the question, and my answer again please. – Maroun Mar 11 '14 at 13:52
  • @MarounMaround exactly, and that is the problem. You said "Since the compiler cannot check if static variables are initialized at compilation time, it gives initial values for them." but the fact that the compiler gives initial values for them has **nothing** to do with the fact that the variable is static. – Erwin Bolwidt Mar 11 '14 at 16:07
  • @ErwinBolwidt But `static` variables gets initialized even if they're inside a method :) Nothing wrong with what I said, but will add your note. – Maroun Mar 11 '14 at 17:00
0

I think the static keyword defaults it n to 0. On your 2nd program you just have "int m"

j.con
  • 865
  • 7
  • 18