Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35602
| Path | csiph.com!news.mixmin.net!news.unit0.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Good pattern for realizing the out-param capability in Java? |
| Date | Wed, 14 Sep 2016 18:43:24 +0200 |
| Lines | 76 |
| Message-ID | <e3td1dF3ufvU1@mid.individual.net> (permalink) |
| References | <ec81f584-4e19-4f0b-ba29-924ef4e1a570@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | quoted-printable |
| X-Trace | individual.net ZvxZwgAQIrVMk20UeuXKUgTxM6RLFwT9F7CS/BIRQ0lAir4Wg= |
| Cancel-Lock | sha1:rj8giiZpRRW10+g9tnzqIHlq+K0= |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 |
| In-Reply-To | <ec81f584-4e19-4f0b-ba29-924ef4e1a570@googlegroups.com> |
| Xref | csiph.com comp.lang.java.programmer:35602 |
Show key headers only | View raw
On 14.09.2016 18:17, sandeep6699@yahoo.com wrote:
> I want to solicit the opinion of the Java experts about realizing
> the out-param capability in Java.
It's been quite a while since I have seen this asked but apparently some
topics do not die.
> Suppose there is a method in a class where
> I would like to realize the following:
>
> class MyClass {
> int myMethod(int inIntParam, int outIntParam, double outDoubleParam) {
> // Accepts inIntParam as input
> // Updates outIntParam and outDoubleParam
> // Returns an int
> …
> }
> …
> }
>
> Since primitive types are passed by values, and primitive type
> wrapper classes are immutable, I am thinking of having outIntParam and
> outDoubleParam as instance variables in the class, which get set prior
> to calling myMethod. Then myMethod can make do with only one parameter i.e.
This is a bad idea. It will break under multithreaded usage. Also,
design wise this state does not belong into the instance. From the
instance's point of view this is purely transitional state which you now
bind into the instance for ever.
> class MyClass {
> protected int outIntParam;
> protected double outDoubleParam;
>
> int getOutIntParam() {
> return outIntParam;
> }
>
> double getOutDoubleParam() {
> return outDoubleParam;
> }
> //
> // No Settors for these instance variables
> // …
>
> int myMethod(int inIntParam) {
> // Accepts inIntParam as input
> // Updates instance variables in outIntParam and outDoubleParam
> // Returns an int
> …
> }
> …
> }
>
> Kindly suggest if there is a preferred way to realize the same.
Your example is pretty generic and does not say much about the real use
case. In absence of more knowledge my advice is: create a class that
contains the necessary state and return an instance of that or receive
an instance of that class as parameter and manipulate it in the method -
whichever is more appropriate for your use case.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Good pattern for realizing the out-param capability in Java? sandeep6699@yahoo.com - 2016-09-14 09:17 -0700
Re: Good pattern for realizing the out-param capability in Java? Robert Klemme <shortcutter@googlemail.com> - 2016-09-14 18:43 +0200
Re: Good pattern for realizing the out-param capability in Java? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-14 12:55 -0400
Re: Good pattern for realizing the out-param capability in Java? j4n bur53 <janburse@fastmail.fm> - 2016-09-14 21:00 +0200
Re: Good pattern for realizing the out-param capability in Java? Eric Douglas <e.d.programmer@gmail.com> - 2016-09-14 12:54 -0700
Re: Good pattern for realizing the out-param capability in Java? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-09-14 17:25 -0400
Re: Good pattern for realizing the out-param capability in Java? j4n bur53 <janburse@fastmail.fm> - 2016-09-14 23:45 +0200
Re: Good pattern for realizing the out-param capability in Java? Lew <lewbloch@gmail.com> - 2016-09-14 16:29 -0700
csiph-web