Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By wsaryada

Thread: variable may not have been initialized...

  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default variable may not have been initialized...

    hello all,

    this error is getting extremely annoying. I simply don't have all of the *rules* written down for control structures and conditional structures. If I did, I wouldn't be writing this code and getting the error. Let me ask this:

    here is some java code that will give me the above error:

    Java Code:
    class tester
    {
       public static void main (String[] args)
       {
    
          String temp1;
          String temp2;
    
          if (1 == 1)
          {
             temp1 = "value";
          }
    
             temp2 = temp1;
       }
    }
    this will error out on the temp2 assignment. can someone please tell me why? what rule about java is doing this? Once again I'm running into my frustrations with Java (and any other language too for that matter), whereby the rules are not spelled out in plain sight anywhere on the web. There's redundant text all over the place unfortunately. Can someone shed some light here for a poor newbie?

    thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: variable may not have been initialized...

    Why not simply give your Strings initial default values? "" would work great, don't you think?

    And no, the compiler is not smart enough to know that if (1 == 1) will always be true.

  3. #3
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: variable may not have been initialized...

    Quote Originally Posted by Fubarable View Post
    Why not simply give your Strings initial default values? "" would work great, don't you think?

    And no, the compiler is not smart enough to know that if (1 == 1) will always be true.
    so giving a string variable an initial value constitutes initialization? that *almost* make sense, but by definition it's not obvious 100%. is that the rule?

    see what I'm getting at...that's a RULE! :) would be nice to have lists of those for every single language. whew.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: variable may not have been initialized...

    Quote Originally Posted by ajetrumpet View Post
    so giving a string variable an initial value constitutes initialization? that *almost* make sense, but by definition it's not obvious 100%. is that the rule?

    see what I'm getting at...that's a RULE! :) would be nice to have lists of those for every single language. whew.
    The rules as I know them are this:
    • all class-level variables are initialized regardless of whether you explicitly initialize them or not according to specific rules in the JLS.
    • String variables and other reference variables (all non-primitive variables) are initialized to null by default.
    • You often don't want to have your variables initialized to null, and so it is a good idea to give your variables initial values on declaration.
    • Local variables (method variables, constructor variables and the like) are not initialized by default.
    • And so you as the programmer *must* initialize *all* local variables before trying to use them.

  5. #5
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: variable may not have been initialized...

    thanks so much!! that's great!! I sure wish there was more of that across the internet. too bad...you get an A+ rating in my book, hows that?? :)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: variable may not have been initialized...

    Quote Originally Posted by ajetrumpet View Post
    thanks so much!! that's great!! I sure wish there was more of that across the internet. too bad...you get an A+ rating in my book, hows that?? :)
    I'll take it. And you're welcome!

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: variable may not have been initialized...

    As Fubarable mention the JLS, you might want to look more on the Java specs on the following link: Java SE Specifications
    Fubarable likes this.

Similar Threads

  1. Variable to initialized
    By Bungie in forum New To Java
    Replies: 10
    Last Post: 11-28-2012, 09:38 AM
  2. Variable may not have been initialized
    By gerchan in forum New To Java
    Replies: 1
    Last Post: 09-06-2012, 02:17 PM
  3. Variable not initialized?
    By seegee in forum New To Java
    Replies: 2
    Last Post: 07-15-2011, 02:00 PM
  4. Variable not initialized.
    By miss_peaches in forum New To Java
    Replies: 14
    Last Post: 04-27-2011, 09:28 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
  •