3
public class memory {
    int b;

    public void main() {
        int a;
        System.out.println(a); /* complie time error */
        System.out.println(b); /* here works how */
    }
}

which assigns a default value to b

Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55
  • 3
    b is a class level variable. Both types (static/instance) of those get default values. Local variables don't. It's how Java is designed – Stultuske Oct 10 '19 at 06:50
  • 3
    Local variables and fields are just treated differently. – Sweeper Oct 10 '19 at 06:51
  • You have this exactly back to front. It is the uninitialised variable declared inside the method that causes the compiler error message. – user207421 Oct 10 '19 at 06:58
  • @user207421 no, it's trying to use the uninitialised variable that causes the error. – Stultuske Oct 10 '19 at 07:05
  • 2
    [jls-4.12.5. Initial Values of Variables](https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12.5) - *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).* – Elliott Frisch Oct 10 '19 at 07:10
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html under section Default Values – mettleap Oct 10 '19 at 07:10
  • No, it is using the uninitialised variable *declared in the method* that is causing the error, which is contrary to the apparent meaning of your title. There are two uninitialised variables here, and only one gives the error. – user207421 Oct 10 '19 at 07:32
  • @user207421 indeed, that's what I'm saying. It isn't the uninitialised variable declared in the method that causes it, it's using said variable. Also: it's not my question, so also not my title :) – Stultuske Oct 10 '19 at 08:08
  • Thanks, Guys. I am asking the default constructor to provide default values(when we creating objects). Here object doesn't play a role. So how instance variable have values. – GOPALAKRISHNAN Oct 10 '19 at 08:21
  • @Stultuske So that's what I said in the first place. To the OP. – user207421 Oct 10 '19 at 08:28
  • @user207421 you said: "It is the uninitialised variable declared inside the method that causes the compiler error message." which is not the case. It's using the variable. – Stultuske Oct 10 '19 at 08:31
  • @GOPALAKRISHNAN You can't initialize a method-local variable in a constructor. Once again you have it back to front. It is the method-local variable that needs initialising, not the instance variable. – user207421 Oct 10 '19 at 08:31
  • @Stultuske Context please. 1. I was addressing the OP. 2. I was telling him that he was looking at the wrong variable. 3. Which he still is. – user207421 Oct 10 '19 at 08:32
  • Similar Discussion - https://stackoverflow.com/questions/1560685/why-must-local-variables-including-primitives-always-be-initialized-in-java – Tutai Kumar Dalal Oct 10 '19 at 10:35

2 Answers2

1

You are using an uninitialized variable within a method and Java requires those to be initialized before you use them. They don’t get a default value. Class member variables and static variables do get a default value but not variables used within your method code.

1

The difference is class members are implicitly initialized, local variables are not.

Each type has a default, and obvious, value. For int it’s 0.

So these two class fields will have the same value are the instance in constructed:

public class memory {
    int field1;
    int field2 = 0;

You can’t access a (potentially) uninitialised variable. So to use a variable/field, no code path must exist that doesn’t result in a value being assigned to the variable.

For class fields, they are assigned the default value is one isn’t specified in the declaration. Local variables do not.

Bohemian
  • 412,405
  • 93
  • 575
  • 722