Article View: borland.public.jbuilder.java.language
Article #8391Re: Simple question for gurus ;)
From: Dave Bird
Date: Tue, 20 Mar 2001 02:14
Date: Tue, 20 Mar 2001 02:14
74 lines
3282 bytes
3282 bytes
In article <Z_tt6.19$xH3.1481@psinet-eu-nl>, John Good
<dumster@collector.org> writes
>I've got a strange problem with Java, it's probably a feature, but please
>point me to solving this.
>
>If I have something like:
>
>public void test(int param) { param = 5;
>}
>
>and practically want to set the param to 5 (I do not want to use return,
>please!), and when I call it from another method, like:
>
>public void example(){
> int example_parameter = 0;
> test( example_parameter );
> System.out.println("parameter: " + example_parameter);
>}
>
>I'll get the 0 as the output, and I want 5; how to assign a value a
>parameter? (pointers I miss you ;)
>I've checked some books on that, but as allways its not in it, because it's
>"too obvious"...
Wow; even in C++ what you have written would not work without
declaring public void test( int ¶m ){.........
That little "&" makes a difference :-> !
In Java variables EXIST as one-word values for a simple numerical
type and EXIST as one-word locations for class or array objects.
If you pass an integer, you pass the numerical value.
If you pass a class-object, you pas the location of the object
as allocated in the calling routine.
This will work:
public class WrapsInt{
public int x;
WrapsInt(){ x=0; };
WrapsInt(int h){ x=h; }
}
public class SomeWork{
...............
public void doFirst(){
WrapsInt exampleParam = new WrapsInt(0);
doInside( exampleParam );
int result = exampleParam.x; ........
};
public void doInside( WrapsInt theParam ){
theParam.x = 5;
.......
};
........
}
'result' will be five, not zero.
It's not really a lot of work because usually, when you want to
change (some fields of) what you passed as parameter, what you
passed will be a class object anyway; not a simple numerical type.
[As best I remember class "Integer" doesn't do what you want,
it has a single int field which has **constant** value].
--
^-^-^-@@-^-;-^ http://www.xemu.demon.co.uk/
(..)__u news:alt.smoking.mooses
happy as a clam at high tide -. <_" .-._.-.
Technical Details
Message-ID:
<1OO$GeKtzrt6EwuU@xemu.demon.co.uk>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!hermes.visi.com!news-out.visi.com!cpk-news-hub1.bbnplanet.com!londen1-snf1.gtei.net!news.gtei.net!newspeer.clara.net!news.clara.net!dispose.news.demon.net!news.demon.co.uk!demon!xemu.demon.co.uk!daveReferences:
<Z_tt6.19$xH3.1481@psinet-eu-nl>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,a90669a07786d662 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-20 10:48:17 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!hermes.visi.com!news-out.visi.com!cpk-news-hub1.bbnplanet.com!londen1-snf1.gtei.net!news.gtei.net!newspeer.clara.net!news.clara.net!dispose.news.demon.net!news.demon.co.uk!demon!xemu.demon.co.uk!dave From: Dave Bird <dave@xemu.demon.co.uk> Newsgroups: borland.public.jbuilder.java.language Subject: Re: Simple question for gurus ;) Date: Tue, 20 Mar 2001 02:14:05 +0000 Organization: none Message-ID: <1OO$GeKtzrt6EwuU@xemu.demon.co.uk> References: <Z_tt6.19$xH3.1481@psinet-eu-nl> NNTP-Posting-Host: xemu.demon.co.uk X-NNTP-Posting-Host: xemu.demon.co.uk:158.152.196.209 X-Trace: news.demon.co.uk 985113834 nnrp-02:24943 NO-IDENT xemu.demon.co.uk:158.152.196.209 X-Complaints-To: abuse@demon.net MIME-Version: 1.0 X-Newsreader: Turnpike Integrated Version 5.00 U <dQumtnY$x4rJ2u5tL5fS$n2vuP> Lines: 75