initializing local variables

I am using eclipse for a program, and i cannot find out why it is saying i did not initalize the variables num and denom:

if(howMany == 1)

{

if(var1 == "A")

{

num = top1;

denom = bottom;

}

if(var1 == "B")

{

num = top2;

denom = bottom;

}

System.out.print("The result is " + num + "/" + denom);

}

}

This is not the whole source, of course. I also put in

int num;

int denom;

all help appreciated!

[514 byte] By [@_-.-_@] at [2007-11-26 12:03:40]
# 1

What happens if howMay is not 1? Or if var1 is neither A nor B?

Also, use equals(), not ==, for comparing Strings.

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverd at 2007-7-7 12:29:10 > top of Java-index,Java Essentials,Java Programming...
# 2
well i didnt put the whole source on there, so ill have to explain that this is a situation in which var1 must be A or B. also, the rest of the source (which isnt perfect yet) will explain what happens when howMany is not 1.
@__@ at 2007-7-7 12:29:10 > top of Java-index,Java Essentials,Java Programming...
# 3
> well i didnt put the whole source on there, so ill> have to explain that this is a situation in which> var1 must be A or B. The compiler can't know that.
jverd at 2007-7-7 12:29:10 > top of Java-index,Java Essentials,Java Programming...
# 4

Indeed, the compiler doesn't know whether things in an if block ever happen. So when it sees code like this:

public class Junk

{

public static void main(String[]args)

{

int a = 1;

//b is uninitialized right now

int b;

//the compiler isn't sure this if block will execute

if(a==1)

{

b=2;

}

//so it's still not sure b was initialized

// and it will tell you "variable b might not have been initialized"

System.out.println("b=" + b);

}

}

it will throw errors. If you change it to have an else that does initialize it, then it knows that either the if or the else had to execute and b must have been initialized. Like this:

public class Junk

{

public static void main(String[]args)

{

int a = 1;

//b is uninitialized right now

int b;

//the compiler isn't sure this if block will execute

if(a==1)

{

b=2;

}

else

b=3;

//COMPILER IS HAPPY NOW!

System.out.println("b=" + b);

}

}

Makes the compiler happy!

ericode at 2007-7-7 12:29:10 > top of Java-index,Java Essentials,Java Programming...
# 5

> Makes the compiler happy!

You have to be careful though. Giving it an aribtrary value just to make the compiler happy is the wrong approach. For instance, if you only allow A or B, but you get C, you should throw an IllegalArgument exception in the else case, not just assign an aribtrary value.

Or if you know there's no way to get C, then you should throw an AssertionError.

If any value is allowed, and there's a reasonable default case for all but the spcecific A or B values, then it's okay to do else { whatever = 3; } .

jverd at 2007-7-7 12:29:10 > top of Java-index,Java Essentials,Java Programming...