Traug
Traug

Reputation: 59

Confusion with variable initialization

Here's a code snippet that won't compile. I get an error on the last line claiming that the dots_per_page variable wasn't initialized. This error goes away when I set it equal to a value on the first line shown. Is there something I'm overlooking? I didn't think that I would have to set it equal to a value upon declaration.

long dots_per_page;
if (print_type == 'T' || print_type == 't') {
dots_per_page = 5000;
}       
else if (print_type == 'I' || print_type == 'i') {
dots_per_page = 10000;
}               
else {
System.out.println("You did not provide a valid option.");
}   
System.out.println(dots_per_page);

Upvotes: 3

Views: 356

Answers (8)

B. Walker
B. Walker

Reputation: 1

else {
System.out.println("You did not provide a valid option.");
}   
System.out.println(dots_per_page);

The coding came very close to working. There was just a small error. In your if-else statement, you print out a statement that says "You did not provide a valid option." Then, you print out dots_per_page. However, if it has gone to the else statement, it cannot print dots_per_page. I would suggest having a print statement in other areas of the if-else statement where you can be sure that you have a dots_per_page to print

Upvotes: 0

Caitlin Craik
Caitlin Craik

Reputation: 131

Try setting dots_per_page to null? With the declaration I mean.

Upvotes: -1

invertigo
invertigo

Reputation: 6438

A lot of these answers are saying that you can't leave a primitive type unset. That is not entirely true. You can leave a primitive type unset all day long, but you can't use that variable until it is set. You are using dots_per_page on your last line in the system.out.println call. If you remove that line, you will also see that your code will compile. So you have multiple options to solve this problem, either initialize your variable for all code paths, or only use that variable in the code paths where it has been initialized.

An example of the second option:

long dots_per_page;
if (print_type == 'T' || print_type == 't') {
dots_per_page = 5000;
System.out.println(dots_per_page);
}       
else if (print_type == 'I' || print_type == 'i') {
dots_per_page = 10000;
System.out.println(dots_per_page);
}               
else {
System.out.println("You did not provide a valid option.");
}   
//System.out.println(dots_per_page); comment out this line

EDIT: and a third option that just came to me: return out of your method in the else case

long dots_per_page;
if (print_type == 'T' || print_type == 't') {
dots_per_page = 5000;
}       
else if (print_type == 'I' || print_type == 'i') {
dots_per_page = 10000;
}               
else {
System.out.println("You did not provide a valid option.");
return;
}   
System.out.println(dots_per_page);

Upvotes: 4

Óscar López
Óscar López

Reputation: 236150

Do this at the beginning:

long dots_per_page = 0;

All local variables must be explicitly initialized in Java, the compiler is complaining because there's an execution path that doesn't assign a value to the variable: if the else condition holds, what's the value of dots_per_page? Alternatively, you could add this to the else block:

else {
    dots_per_page = 0;
}

The point is: a value must always be assigned to a local variable if it's going to be read, no matter what execution path is run. Only attributes have default values.

Upvotes: 1

pandorym
pandorym

Reputation: 485

If print_type is a String, it needs to be in double quotes, and you need to use the ".equals()" method to compare them. As mentioned, if it is a char your code should work correctly.

Also, if the result isn't either "I" nor "T' (or their lowercase versions), dots_per_page doesn't have a value at the end of the program.

else {
    System.out.println("You did not provide a valid option.");
    dots_per_page = -1;
};

Something like that (an impossible return), would show you that something went wrong.

Initializing variables that you know your program should change with a value that they shouldn't be, helps show if your program is running correctly.

Upvotes: 1

James Allman
James Allman

Reputation: 41208

Java Tutorial: Primitive Data Types

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

Data type default values

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

Upvotes: 3

Raystorm
Raystorm

Reputation: 6568

dots_per_page is a primitive type and as such is not allowed to be unset. just use long dots_per_page =0; or long dots_per_page = -1; at the beginning.

the other option is to add dots_per_page = 0; in your else block.

Upvotes: 0

kichik
kichik

Reputation: 34744

You have a case where dots_per_page never gets a value. If print_type is not 'T', 't', 'I', or 'i', then "You did not provide a valid option" is printed and the value is not set.

If you set a default value in the first line, this case will have the default value and all is well. You can also set a different value in the last else block.

Upvotes: 6

Related Questions