Results 1 to 2 of 2
  1. #1
    gerchan is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Variable may not have been initialized

    Hello and good afternoon.
    I have just begun learning java a few days ago so apologies if this is a very dumb question. So I was doing the following program and when I it I got the error the variable (monthdays) may not have been initialized. Thereafter I decided to assign it a value of 0 and the program worked. When I researched the net I realized that the problem had something to do with local and class variables unfortunately I couldn't exactly understand how it came about. Can someone please explain in laymen terms why the error message came about. Thanks for your help and have a good day.

    Java Code:
    class MonthDaysAssignment
    {
      public static void main (String Args[])
      {
      int monthnum = 2;
      int yearnum = 2001;
      int monthdays =0; //variable monthdays initialized
      
      switch (monthnum)
        {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12: monthdays = 31;
        break;
        case 4: case 6: case 9: case 11: monthdays =30;
        break;    
        }
        if (yearnum%4==0)
        {
        monthdays = 29;
        }
        else if (yearnum%4!=0)
        {
        monthdays = 28;
        }                            
        {
        System.out.println (monthdays+" days");
        }
      }
    }
    P.S. The problem was in line 7

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Variable may not have been initialized

    It would help if your indentation was correct, but if you don't initialise monthdays with a value then there is a route through your code in which monthdays might not get set. Since the println statement at the end tries to use monthdays, then it would be possible for the code to get there without a valid value in that variable. This is not allowed by the compiler.

    One way around it would be to change the case statement to have a default fall-through representing, say, the 30 day months or the 31 day ones. That way you know it has to be set. Actually, not sure your logic for February is correct, but it's hard to tell with the current layout.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Variable not initialized?
    By seegee in forum New To Java
    Replies: 2
    Last Post: 07-15-2011, 02:00 PM
  2. Variable not initialized.
    By miss_peaches in forum New To Java
    Replies: 14
    Last Post: 04-27-2011, 09:28 AM
  3. variable name might not have been initialized
    By p0rnstar in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 03:27 PM
  4. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 AM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •