why variable have to be initialized?

here i tried to get output month by inputting month number but why i am having error - "monthString" mightn't have been initialized ? - and why i am not getting output string from " monthString "?

why monthString have to be initialized ?

import java.util.Scanner;
public class SwitchClass {

public static void main(String[]args)
{
   Scanner input = new Scanner(System.in);
   System.out.printf(" when did u born ? ");
   int monthNumber = input.nextInt();
   String monthString ;

switch (monthNumber)
{
    case 1:
    monthString = "January ";
        break;
    case 2:
        monthString = "February ";
        break;
    case 3:
        monthString = "March ";
        break;
    case 4:
        monthString = "April ";
        break;
    case  5:
        monthString = "May";
        break;
    case  6:
        monthString = "June";
        break;
    case  7:
        monthString = "July";
        break;
    case  8:
        monthString = "August";
        break;
    case  9:
        monthString = "September";
        break;
    case  10:
        monthString = "October";
        break;
    case  11:
        monthString = "November";
        break;
    case  12:
        monthString = "December";
        break;
   }
System.out.println(monthString);       }

 }

Answers


What if monthNumber is not between 1 and 12? In that case, monthString won't be initialized. You should give it some default value when you declare it :

String monthString = null; // or ""

It would be a good idea to add a default case to your switch statement.

Example:

switch (monthNumber) {
    case 1: monthString = "January";
        break;

    //other cases...

    default: monthString = "Invalid Month Number";
        break;
}

This way if monthNumber is not 1-12 then there is still a default case for the switch statement to flow to.


May be this link will help to get proper understanding.

http://stackoverflow.com/questions/5478996/should-java-string-method-local-variables-be-initialized-to-null-or

monthString is a local variable within main(), therefore, it must be initialized to prevent the compiler error.

If monthString were a Class variable then it does not have to be initialized explicitly.

You can do this by moving monthString outside of main() and declare it as:

static String monthString;


Because the designer of Java language believes it made more sense for it to be! Codes are easier to read when variables are initialised. A statement String foo; feels non-deterministic because you have to guess what's the default value of String is whereas String foo = null; is more deterministic.

To give you a more obvious example, consider this:

int x;
Int y;

Can you very quickly guess what the default values are? You probably have to pause for a few second to realize x is probably 0 and y is probably null


Local variables MUST always be initialized before use. For Java 6, the compiler doesn't consider the "Incomplete" initialization of variables within flow control blocks and try-catch blocks. The initialization must be done for all cases:

If - else:

String s;
int a = 10;
if(a > 5){
 s = "5";
}else{
 System.out.println("");
}    
System.out.println(s); // error if s isn't initialized within both if and else blocks

While loop:

String s;
int a = 10;
while(a > 0) {
  s= ">0";
  a--;
}    
System.out.println(s);// error if s isn't initialized outside the while

Try-Catch block:

String s;
try {
 s = "";
    } catch (Exception e) {}
System.out.println(s);// error if s isn't initialized within both try and catch blocks

Switch block:

String s;
int a = 10;
switch (a) {
     case 10: 
         s="10";
         break;
     default:
         break;
         }
System.out.println(s);// error if s isn't initialized all cases, default case included

Initialize the variable before the switch and all will be fine.

String monthString = "";

Need Your Help

Codeception bash color output does not display

bash powershell cmd cygwin git-bash

I have been testing with Codeception and PhantomJS for a while, but when ever i have to debug and run

Know which item selected?

android button alertdialog

I have an application where you can select an item from a AlertDialog spinner like, but I don't know how to make my app, to memorize the selected choice, and then behave my button to this. This is ...