Local variable initialization

c local variable initialization
variable initialization in c++
local variable in java
c++ static local variable initialization
instance variable
instance variable vs local variable
instance variables java
types of variables in java with examples

Why is the compiler is happy that variable g is initialized in if(true) statement while complains regarding g initialization in if(gg) case, throwing the following error:

Error:(11, 28) java: variable g might not have been initialized

  public static void main(String args[]) {
    boolean gg = true;
    int g;
    if (gg)
    //  if (true)
    {
        g = 6;
    }
    System.out.println(g);
}

true is a constant expression, the compiler can deduct its value compile time.

On the other hand, gg is not constant, but a variable, that you could initialize with anything (even an expression, a return value of a function, etc). So, the compiler does not check how did you initialize that variable, is assumes that it can be anything.

That's why the compiler thinks that the variable g can or cannot be initialized.

How deep do you think the compiler can go? Take this one, for example:

boolean gg = Math.random() >= 0.5;
gg = gg ? gg : true;

We know that gg will be true, but how does the compiler know it?

Or another example:

boolean gg = (Math.random() >= 0.0);

Again, we know that it is always true (because random() is between 0.0 and 1.0), but the compiler doesn't know.

So, it's not an easy task for a compiler to do, therefore the compiler creators decided to check only the constant expressions when predicting execution flow, and not to guess the variable values.

That's the reason why you get a warning in one case and don't in the other.

local variable initialized to zero in C, Have you ever wondered why instance fields don't have to be initialised and default to 0, false, null etc, while local variables have to be  Initializing variables with initializers in Java. int x = 5, y = 10; When you declare two class or instance variables in a single statement but use only one initializer, you can mistakenly think But the initializer applies only to y , so x is initialized to its default value, 0 . (If you make

This is due to the difference between declaration and initialisation.

The part before the equals sign is the declaration boolean gg or int g and the initialisation is the = true.

In the case above, gg is always initialised but g is only initialised if gg is true. This can only be known by running the code to determine gg's value (we can tell it's true by running through the code manually), hence we get a complaint from the compiler the g variable may not be initialised.

Mandatory Local Variable Initialization - David Klempfner, All non-local variables with static storage duration are initialized as part of program startup, before the execution of the main  Different ways of initializing a variable in C++ Method 1 (Declaring and Initializing a variable). Method 2 (Initializing a variable using parenthesis). Yes, they’re the same. On the other hand, for a class type they’re Method 3 (Initializing a variable using braces). Method 4 (Declaring a

The compiler will most likely optimize your if (true) condition to no condition at all (try and de-compile your .class, you should see the if block gone).

However, it is not as foreseeing when it comes to even local variable references with no possible changes within context, so it won't "optimize" for that, a sign that it will not predict that your condition will always be true.

As such it leaves you with a local variable initialized only at a condition (regardless of how factually that condition can only be true at runtime in this specific context), and referenced later by your println invocation, with the "risk" of not actually having been initialized.

That in turn, causes the "eager" compiler error.

Initialization, Following are valid examples of variable declaration and initialization in Java Local variables are visible only within the declared method, constructor, or block. As most of the answers said by putting the constraint of initialising the local variable, it is ensured that the local Instance variables can be hidden by declaring local variables (same name) - to ensure expected behaviour, local

Java - Variable Types, does not have a defined value, hence it cannot be used until it is assigned such a value. Initialize your variables. Initialize your variables upon creation. You may eventually find cases where you want to ignore this advice for a specific reason (e.g. a performance critical section of code that uses a lot of variables), and that’s okay, as long the choice is made deliberately.

Initializing a variable, is whatever (garbage) value happens to already be in that memory location! 1) If permitted, Constant initialization takes place first (see Constant initialization for the list of those 2) For all other non-local static and thread-local variables, Zero initialization takes place. In practice, variables

1.6, declaration. Its scope is only limited to the for loop. It can be used to reserve as well as initialize one or more bytes. Allocating Storage Space for Initialized Data. The syntax for storage allocation statement for initialized data is − [variable-name] define-directive initial-value [,initial-value] Where, variable-name is the identifier for each storage space. The assembler associates an

Comments
  • gg is not a compile-time constant, so compiler doesn't know that gg is true. The rules for determining if g is definitely-assigned in the print statement are well-documented, and takes up an entire chapter in the Java Language Specification.
  • Compiler can't be sure gg is always true (it can reassign somewhere in code), there are couple of answers in site
  • Because the compiler doesn't keep a state, so that it knows if gg is always going to be true. It sees that you have a Boolean and that it may be false in some cases (otherwise why would you have an if statement?). If gg is false, then g will be uninitialized and your System.out.print() statement will produce an error
  • See stackoverflow.com/questions/9082971/…