Path: csiph.com!news.mixmin.net!news.unit0.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme 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: References: 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: Xref: csiph.com comp.lang.java.programmer:35602 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 > =E2=80=A6 > } > =E2=80=A6 > } > > 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,=20 design wise this state does not belong into the instance. From the=20 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 > // =E2=80=A6 > > int myMethod(int inIntParam) { > // Accepts inIntParam as input > // Updates instance variables in outIntParam and outDoubleParam > // Returns an int > =E2=80=A6 > } > =E2=80=A6 > } > > 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=20 case. In absence of more knowledge my advice is: create a class that=20 contains the necessary state and return an instance of that or receive=20 an instance of that class as parameter and manipulate it in the method - = whichever is more appropriate for your use case. Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/