This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 186
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: 39286
745
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 Foreman
Posts: 3254
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.
 
Sheriff
Posts: 13480
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: 64496
225
  • 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.
 
Sheriff
Posts: 6789
469
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: 2287
95
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 Foreman
Posts: 3254
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: 186
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!
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!