Java instance variables initialization with method

前提是你 提交于 2020-01-11 04:30:25

问题


I am a little bit confused about the following piece of code:

public class Test{

  int x = giveH();
  int h = 29;

  public int giveH(){
     return h;
  }

  public static void main(String args[])
  {
      Test t = new Test();
      System.out.print(t.x + " ");
      System.out.print(t.h);          
  }
}

The output here is 0 29, but I thought that this has to be a compiler error, because the variable h should have not been initialized when it comes to the method giveH(). So, does the compilation go through the lines from top to bottom? Why is this working? Why is the value of x 0 and not 29?


回答1:


The default value of int is 0 (see here). Because you initialize x before h, giveH will return the default value for a int (eg 0).

If you switch the order like this

int h = 29;
int x = giveH();

the output will be

29 29



回答2:


Compilation in Java doesn't need the method to be declared before it is used. The Java tutorial goes into a bit more detail on initialization.

Here's a way to think about it: the compiler will make a note to look for a method called giveH somewhere in scope, and it will only error if it leaves the scope and doesn't find it. Once it gets to the giveH declaration then the note is resolved and everyone is happy.

Also, the variable initialization for instance variables in Java is moved to the beginning of the constructor. You can think of the lines above being split into two parts, with the declaration for x and h above, and the assignment inside the constructor.

The order of declaration does matter in this case. When the variable x is initialized, h has the default value of 0, so giveH() will return that default value. After that, the variable h is given the value 29.

You can also look at the Java Language Specification sections on Field Initialization and Forward References During Field Initialization.




回答3:


@Maloubobola has provided the correct answer, but since you don't seem to fully understand yet, let me try to elaborate.

When Test is created, it runs the variable initialization (x = giveH(), h= 29) once. Your misunderstanding may be that variable x is always determined by giveH(), whereas it only determines it's value when x is initialized.

That's why the order of the statements is crucial here; x is initialized before h, and therefore h is 0 when giveH() is called at x's initialization.




回答4:


It's bad practice to use a method in the field initializer. You can fix this by making h final. Then it will be initialized when the class is loaded.

import java.util.ArrayList;

public class Test {
    int x = giveH();
    final int h=29;

    final public int giveH(){
        return h;
    }

    public static void main(String args[]) {
        Test t = new Test();
        System.out.print(t.x + " ");
        System.out.print(t.h);          
    }
}



回答5:


If we do not initialize the value for non static variables like x in this program JVM will provide default value zero for class level non static variables.




回答6:


Once declared, all the array elements store their default values. Elements in an array that store objects default to null. Elements of an array that store primitive data types store: 0 for integer types (byte, short, int,long); 0.0 for decimal types (float and double); false for boolean; \u0000 for char data.

Actauls values will come only when we give initialize them



来源:https://stackoverflow.com/questions/33669037/java-instance-variables-initialization-with-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!