This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes return int in parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return int in parameter" Watch "return int in parameter" New topic
Author

return int in parameter

Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
I have a method that needs to return an int in a parameter. In C++, I would just pass the address of my parameter, and have the called method populate the contents of that address.
In Java, I am not sure what to do, since I can not pass the address of the int. Do I need to create a class just for the int value so I can pass the class (a reference) and have the called method populate the value (class.int = value).

This seems like a lot of work for just wanting to return an int value. What is ther correct way of accomplishing this?
Thanks.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Rob
Is there a there a reason that your method can't just return it?

then the call would look like this:
int i = myMethod();


Dave
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Originally posted by Dave Vick:
Rob
Is there a there a reason that your method can't just return it?

then the call would look like this:
int i = myMethod();

Thanks, but I am already returning something, so the other return needs to be through a parameter.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60755
    
  65

This should be a red flag that you might need to refactor how you are doing things -- perhaps you are still thinking in C++ rather than Java.
Why not give us some more details regarding what your method is trying to accomplish and the context in which it is used?
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Originally posted by Bear Bibeault:
This should be a red flag that you might need to refactor how you are doing things -- perhaps you are still thinking in C++ rather than Java.
Why not give us some more details regarding what your method is trying to accomplish and the context in which it is used?
hth,
bear

I have a method that returns an error code. Additionally, I have to return an int value, so I figure an output parmaeter is the way to go. But since only objects pass references, not primitive types, I am just asking if I have to create an object that holds an int simply to return an int as a parameter.
int my_method(param1) {
param1 = 5;
return 0;
}
This will not work of course because param1 will not be modified in the call, but it seem silly to have to create a class just to hold an int value.
This must be done all the time, does anyone know what the typical solution is?
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1744
    
    2
Java doesn't have "output parameters." What's an "error code"?
Like others have suggested, if you post some actual code, maybe someone can come up with a Java solution.
Maybe breaking your method into several smaller methods or using exceptions instead of "error codes" would do the trick.
John Dale
Ranch Hand

Joined: Feb 22, 2001
Posts: 399
When I started Java, I was concerned about the same problem. One way:

I often returned values via parameters in C and C++. But in my Java code, several idioms seems to hve replaced the need return values via parameters.
  • The checked exception usually provides a better way to return "error codes".
  • The need to return several values, or a value and its length, largely goes away when you return objects which include everything you need to know about them, or modify objects passed as parameters.
  • The need for method to return in indicator to control iteration, as well as a value via a parameter is used less often in favor of Enumeration, Iterator, and similar devices.
  • (Edited to fix list tag)
    [ June 04, 2002: Message edited by: John Dale ]
    [ June 04, 2002: Message edited by: John Dale ]
    Rob Levo
    Ranch Hand

    Joined: Oct 01, 2000
    Posts: 167
    An array of one primitive, perfect, thanks!
    But you all made a good point in that I should start thinking more in Java and get out of C++ mode.
    I will do that, and thanks again for al the help here.
    Michael Matola
    whippersnapper
    Ranch Hand

    Joined: Mar 25, 2001
    Posts: 1744
        
        2
    Fowler has the refactoring replace error code with exception.
    Jamie Robertson
    Ranch Hand

    Joined: Jul 09, 2001
    Posts: 1879

    This link has some suggestions. The most relevant to you would be the section on "Ways to avoid needing pass-by-reference" and "techniques to eliminate multiple returns from your desired method interface".
    Jamie
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by Rob Levo:
    [...]it seem silly to have to create a class just to hold an int value.

    It might seem silly, but it might also be the first step to a new class with more responsibility.
    For example, you could move the method itself into the new class and split it into several ones - like in a combination of http://www.refactoring.com/catalog/separateQueryFromModifier.html and http://www.refactoring.com/catalog/replaceMethodWithMethodObject.html

    This must be done all the time, does anyone know what the typical solution is?

    There is no *typical* solution - the appropriate solution will heavily depend on the context. I suspect that there is in fact a new class waiting in your code to materialize, but to be sure we surely need to know more about what you actually want to accomplish. Can you post some of your *real* code?


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    it seem silly to have to create a class just to hold an int value.

    In addition to the solutions suggested so far, it's also possible that you don't need a new class at all - you might be able to simply add an instance field to an existing class (most likely the one that contains the method you're writing.) It all depends on which classes need the info, and what the responsibilities of the existing classes are.
    But if the data you're trying to return is an "error code" I'd agree that most likely you should be using exception handling for this sort of thing anyway. The only time this would not apply is if some of the "errors" signalled are really warnings - meaning that they should not actually interrupt the flow of execution, but they indicate possible problems that should be logged for investigation. In this case, it's probably appropriate to make sure your class has a logging mechanism, so that the warnings can be logged as they occur in the method, rather than passing them up to the calling method.


    "I'm not back." - Bill Harding, Twister
    Rob Levo
    Ranch Hand

    Joined: Oct 01, 2000
    Posts: 167
    Thanks Jim. As usual, a big help!
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by Rob Levo:
    Thanks Jim. As usual, a big help!

    So, what did you actually do? It would be really nice to know how you applied the help and in which way it really did help. In this way, we could *all* learn from this experience...
    Rob Levo
    Ranch Hand

    Joined: Oct 01, 2000
    Posts: 167
    1) I will use the two levels of errors that Jim described. For "warning" type errors where the flow of execution can continue, I will use logging. For more serious errors, I will use exception handling.
    2) For primative parameters that need to have their value changed in the called method, I will use an array of one element so that a reference will be passed. If the single primative becomes more than that going forward, I will create a class and pass that object.
    Thanks again for all the help.
    Javaranch continues to be a great resource!!
    Hopefully I will be answering more questions in the future than I am currently asking, lol.
    Rob
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: return int in parameter
     
    Similar Threads
    JTable problem
    refrance variable clearance required..
    Parameter of a method - a method
    How to read a particular record?
    Populate multiple pojo's.