Java instance variable declare and Initialize in t

2019-01-12 12:43发布

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;
     }

}  

5条回答
看我几分像从前
2楼-- · 2019-01-12 12:48

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.

查看更多
小情绪 Triste *
3楼-- · 2019-01-12 12:49

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:

查看更多
Luminary・发光体
4楼-- · 2019-01-12 12:56

You need to do :

int instanceInt = 100;

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

查看更多
何必那么认真
5楼-- · 2019-01-12 13:04

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.

查看更多
老娘就宠你
6楼-- · 2019-01-12 13:13

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 :

On the same topic :


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

查看更多
登录 后发表回答