Results 1 to 5 of 5
  1. #1
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default variable initialization problem

    Hi guys.........

    I have a small problem, basically fundamental problem.

    public class Compile15 {
    public static void main(String args[]){
    int i1=9; //3
    int i2; //4
    if(i1>3){
    i2=8;
    }
    System.out.println(i2);
    }
    }


    In this above program when I try to run this program, it occur compile time error -- "variable i2 might not have been initialized" ,although within if condition I have initialized i2.

    Now if I initialize i2=0 at line 4 it runs perfectly or at line 3 if I declare variable i1 as final int i1 without changing at line 4 then it will compile and run also.

    I'm not so cleared about these two solutions. Please someone clarify this for me in details.

    Thank you................

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    The line i2=8 is not initialisation. It is plain old assignment. Initialisation is giving a variable an initial value when you declare it. For local variables you must intialise them yourself. Whereas for instance variables if you do not initialise them they are intialised by the compiler with a default value.

    So why does the compiler complain? What happens when the if statement is false? What value does i2 have then?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    The compiler doesn't do any interpretation on (the source of) your problem. It just checks that i2 is only initialized if an if-condition succeeds; if it doesn'out succeed, that variable isn't initialized; you, on the other hand, do perform some form of interpretation and you can conclude that that variable must always be initialized because that if-condition always succeeds. If you make that other variable final, the compiler 'knows' the value of that variable and it can deduce that the if-condition succeeds and your variable i2 is always initialized (so it doesn't complain).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Lets consider several other versions of your code.
    Java Code:
    // version 1
    int i1=9;
    int i2;
    i1 = 0;
    if(i1>3){
        i2=8;
    }
    System.out.println(i2);
    
    // version 2
    int i1=0;
    int i2;
    if(i1>3){
        i2=8;
    }
    System.out.println(i2);
    
    // version 3
    int i1=9;
    int i2;
    i1 = 0;
    if(someMethodThatReturnsBoolean()){
        i2=8;
    }
    System.out.println(i2);
    For the first 2 versions we can see that the if statement will not be entered. With the 3rd version we have no idea if it will enter or not unless we have the source of the method as well. Now what the compiler needs to do is have consistent behavior across all 3 versions as well as yours. I certainly would not trust a compiler if it behaves differently just because you want it to.

  5. #5
    mitra is offline Member
    Join Date
    Jun 2011
    Posts
    59
    Rep Power
    0

    Default

    Thanks everybody........
    Now I understand why it happened.

    Thanks again...............

Similar Threads

  1. return new variable -problem
    By Hevonen in forum New To Java
    Replies: 7
    Last Post: 12-08-2008, 06:07 AM
  2. Replies: 1
    Last Post: 11-17-2008, 09:32 AM
  3. variable initialization
    By rqal.10 in forum New To Java
    Replies: 5
    Last Post: 06-15-2008, 06:39 AM
  4. initialization value problem
    By ravian in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 10:54 AM
  5. Icon initialization problem
    By saz25 in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 10:37 PM

Tags for this Thread

Posting Permissions

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