• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Do returned values from methods need to be saved in static variables?  RSS feed

 
Ranch Hand
Posts: 177
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do returned values from methods need to be saved in static variables?


is declared at the top



i couldnt get it working any other way.
 
author & internet detective
Posts: 39244
727
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wayne,
No. A local variable would work just fine:

 
Ranch Hand
Posts: 3218
19
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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)


 
Marshal
Posts: 64179
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case, I would consider the principle of reusing ready‑made code and use the Math.max() method:-Of course, if you are required to wear shorts, that sort of code may be frowned upon.
 
Marshal
Posts: 6697
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wayne, I am interested to know, how the text reads for this exercise? May you please share?
 
Bartender
Posts: 2232
94
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:Do returned values from methods need to be saved in static variables?...


I think this question stems from how can one access the method's returned value.

Instead of doing this:


you can do this:
 
Mike Simmons
Ranch Hand
Posts: 3218
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
wayne brandon
Ranch Hand
Posts: 177
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!