Java instance variable declare and Initialize in t

2019-01-12 12:50发布

问题:

Hi I'm having problem with initialization in java, following code give me compile error called : expected instanceInt = 100; but already I have declared it. If these things relate with stack and heap stuff please explain with simple terms and I'm newbie to java and I have no advanced knowledge on those area


public class Init { 

int instanceInt;  
instanceInt = 100;

   public static void main(String[] args) {

     int localInt;
     u = 9000;
     }

}  

回答1:

You can't use statements in the middle of your class. It have to be either in a block or in the same line as your declaration.


The usual ways to do what you want are those :

  • The initialization during the declaration

    public class MyClass{
        private int i = 0;
    }
    

    Usually it's a good idea if you want to define the default value for your field.

  • The initialization in the constructor block

    public class MyClass{
        private int i;
        public MyClass(){
            this.i = 0;
        }
    }
    

    This block can be used if you want to have some logic (if/loops) during the initialization of your field. The problem with it is that either your constructors will call one each other, or they'll all have basically the same content.
    In your case I think this is the best way to go.

  • The initialization in a method block

    public class MyClass{
        private int i;
        public void setI(int i){
            this.i = i;
        }
    }
    

    It's not really an initialization but you can set your value whenever you want.

  • The initialization in an instance initializer block

    public class MyClass{
        private int i;
        {
             i = 0;
        }
    }
    

    This way is used when the constructor isn't enough (see comments on the constructor block) but usually developers tend to avoid this form.


Resources :

  • JLS - Instance Initializers

On the same topic :

  • Use of Initializers vs Constructors in Java
  • How is an instance initializer different from a constructor?

Bonus :

What does this code ?

public class MyClass {
    public MyClass() {
        System.out.println("1 - Constructor with no parameters");
    }

    {
        System.out.println("2 - Initializer block");
    }

    public MyClass(int i) {
        this();
        System.out.println("3 - Constructor with parameters");
    }

    static {
        System.out.println("4 - Static initalizer block");
    }

    public static void main(String... args) {
        System.out.println("5 - Main method");
        new MyClass(0);
    }
}

The answer



回答2:

Put it in an initializer block.

public class Init { 

  int instanceInt;  
  {
    instanceInt = 100;
  }

  public static void main(String[] args) {
    int localInt;
    int u = 9000;
  }   
}  

Or simply initialize while declaring it:

public class Init { 

  int instanceInt = 100;

  public static void main(String[] args) {
    int localInt;
    int u = 9000;
  }   
} 

References:

  • Java Tutorial - Initializing Fields
  • Java - Initializer Block


回答3:

You can not have a separate statement to assign values to class members in the declaration area. You have to do this from a function, typically a class method.

For your case, consider doing the assignment as soon as you declare.



回答4:

You need to do :

int instanceInt = 100;

If it was static you could initialize in a static block.



回答5:

According to the Java Language Specification:

A class body may contain declarations of members of the class, that is, fields (§8.3), classes (§8.5), interfaces (§8.5) and methods (§8.4). A class body may also contain instance initializers (§8.6), static initializers (§8.7), and declarations of constructors (§8.8) for the class.

However, the statement

instanceInt = 100;

is none of those things, therefore it is not allowed as part of a class body. What you need to do is this:

int instanceInt = 100;

This is allowed because it is a field declaration that includes a variable initializer.