[664] in java-interest

home help back first fref pref prev next nref lref last post

Re: Java compiler is trying too hard for me

daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Wed Jul 12 15:59:00 1995

Date: Wed, 12 Jul 1995 11:25:07 -0800 (PDT)
From: Arthur van Hoff <Arthur.Vanhoff@Eng.Sun.COM>
Reply-To: Arthur van Hoff <Arthur.Vanhoff@Eng.Sun.COM>
To: Tientien Li <li@deming.Jpl.Nasa.Gov>
Cc: java-interest@java.Eng.Sun.COM


Hi Tientien,

> The Java compiler "javac" won't let me do the following type of code:
> 
>    class smartjavac {
>       public void init() {
>          int x;
>          for (int i=0; i<3; i++) {
>             if (i == 0)  x = i;
>             else if (i == 1)  x = i;
>             else x = i;
>          }
>          System.out.println("ValueOfX is " + x );
>       }
>    }
> 
> The compiler print out the following message and won't generate 
> any class code.
> 
>    smartjavac.java:9: Variable x may not have been initialized.
>            System.out.println("ValueOfX is " + x );
>                                             ^
>    1 error
> 
> I've tried several compiler options including "-nowarn", but got the
> same result.
> 
> Any fix for this problem?

You are required to initialize x. Just change the declaration of x
to "int x = 0;".

Have fun,

	Arthur van Hoff
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post