2

I have Java two programs.

Program One

class Ideone
{
    public static void main (String[] args)
    {

        double price = 10;
        String model;
        if (price > 10)
            model = "Smartphone";
        else if (price <= 10)
            model = "landline";
            System.out.println(model);
    }
}

Output :- variable model might not have been initialized(Error)

Program Two:-

class Ideone
{
    public static void main (String[] args)
    {
           double area = 10.98;
           String color;
           if (area < 5)
               color = "red";
           else
               color = "blue";
           System.out.println(color);
    }
}

Output:- blue

My question here is since both the programs are almost similar, why am I getting variable might not have been initialized ?

If the first program is having the problem, should not the 2nd program also throw the same error ?

1
  • color will always be initialized, model will be initialized only if something, that's how the compiler sees it. Commented Oct 16, 2015 at 8:58

6 Answers 6

4

Compiler is smart enough.

In second case you full fill all the conditions. There is an else part to save after all the conditions.

But in your first case there is no else part. Which mean if no conditions is pass, you have a blank field there.

You must either provide an else which make sure the initialization part

double price = 10;
        String model;
        if (price > 10)
            model = "Smartphone";
        else if (price <= 10)
            model = "landline";
        else
           model = null

or provide a default value null or as you want.

double price = 10;
        String model =null;
        if (price > 10)
            model = "Smartphone";
        else if (price <= 10)
            model = "landline";
Sign up to request clarification or add additional context in comments.

Comments

4

In the first case, change the else if to just else. The Java compiler isn't smart enough to realize that one of the two conditions will 100% be executed.

2 Comments

+1 for Java compiler isn't smart enough to realize that one of the two conditions will 100% be executed.
Compiler is smart. That's why it created an error to make sure that there is always an initialization part.
2

In the first code snippet, although logically speaking either the if or the else must occur, the Java compiler does not "know" this. It presumes that it might be possible for both if statements to fail, and therefore gives you a warning message. Here is a breakdown of what is happening in the code:

double price = 10;
String model;
if (price > 10)
    model = "Smartphone";
else if (price <= 10)           // the Java compiler doesn't "know" that one
    model = "landline";         // of these two cases will always occur
    System.out.println(model);

If you change the code to the following you will not get this warning anymore:

double price = 10;
String model;
if (price > 10)
    model = "Smartphone";
else {
    model = "landline";         // in case the if fails, this else will ALWAYS
    System.out.println(model);  // happen, so 'model' is guaranteed to get
}                               // intiailized

In your second code snippet, you use an explicit if-else for initialization, the compiler therefore remains silent on this point.

Comments

1

In the first case you need to initialize since if both the conditions are false (if and else if ) then what would be the default value to be displayed. Add an else part in ur first code and it would not give such warning.

In the second case when the first part (if (condition) == false) then else part will be executed and hence there is no such condition under which the variable color won't have any value.

Comments

1

The problem is that, in your first program you can see that you have used an if-else if statement, the compiler can’t be sure whether these conditions will evaluate to true or false, resulting in non initialization of the variable,hence you get the error.

But in your 2nd program you can see, you have used if-else statement, so in this case, the compiler knows, if the if condition does not satisfy then it(the compiler) can execute the else statement and give the value to the varaible.

So just put this in your 1st program

String model;
if (price > 10)
model = "Smartphone";
else 
model = "landline";
System.out.println(model);

I removed the if part of else-if just so that you know what I did

Comments

0

My question here is since both the programs are almost similar, why am I getting variable might not have been initialized ?

Actually they are not same. You are having a field name model which is not initialized like:

String model = null;

Compiler is considering the case when it is not initialized by any of your if conditions and that is why it is complaining. In first case else is missing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.