Well, a local variable there doesn't work fine - but the reason why is because of program errors, not because static is the answer. When you get the compiler error "Variable 'largestNumber' might not have been initialized", that's telling you something important. Your if / else if structure lists three possible branches - but you haven't actually considered all the possibilities. Consequently the compiler is saying hey, what if none of these conditions is true? What should be the value of largestNumber then?
One possible fix is to give largestNumber a value at the beginning, e.g. "int largestNumber = 0;", or "int largestNumber = Integer.MIN_VALUE;" The reason your static variable works is because you've made it into a static field, and static fields and instance fields are implicitly initialized to 0, while local variables have no implicit initial value. But that's just an accidental solution of the compiler problem, not a logical solution.
This doesn't really fix the problem, as your method should never actually return 0 (unless v1 or v2 or v3 is actually zero, and everything else is negative). You need to study the logic of your if / else conditions more carefully, to make sure you're really considering all the possibilities. Try to organize it so that the last option is simply an "else", with no condition:
That way the compiler knows that if the first two conditions fail, the only remaining possibility is that v3 is largest, so largestNumber definitely gets assigned a value.
Also, on a separate note - why is the method name isBiggerThan1000? That seems needlessly confusing.
Mike Simmons wrote:why is the method name isBiggerThan1000? That seems needlessly confusing.
Indeed. And for a couple of reasons:
1. Violates the Principle of Least Astonishment (POLA) - One would expect a method named isBiggerThan1000 return value would be a boolean true or false instead of an int.
2. There is nothing in the code that hints at 1000 as the basis for comparison. Why would 1000 be in the name of the method when that value is not even considered in any of the logic inside the method?
When I see isBiggerThan1000(1, 5, 999), I would expect the result to be false because none of the arguments are bigger than 1000. Instead, I believe your intent is to return 999, the largest value. What would make more sense is something like this: largestOf(1, 5, 999)
salvin francis wrote:I think this question stems from how can one access the method's returned value.
I'm pretty sure it came from trying to compile something very similar to the code Jeanne posted, and trying to deal with the compilation error it would give. That problem can be "fixed" by making the variable a static field, but that's not a good solution overall. See above.
posted 3 weeks ago
Liutauras Vilda it was just an exercise i dreamed up myself, im really sorry the method name was something i was doing earlier and didnt change the name, i was just getting the largest of the three numbers. Sorry again for the confusion. This was great reading though thanks everyone!
no wonder he is so sad, he hasn't seen this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java