Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

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

 
Ranch Hand
Posts: 264
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: 39462
768
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wayne,
No. A local variable would work just fine:

 
Master Rancher
Posts: 3322
31
  • 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.
 
Sheriff
Posts: 13731
228
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 65474
249
  • 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: 7085
491
Mac OS X VI Editor BSD Linux
  • 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: 2348
102
Google Web Toolkit Eclipse IDE 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
Master Rancher
Posts: 3322
31
  • 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: 264
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!
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!