Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Programmer > Re: method para...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 7 Topic 52653 of 54894
Post > Topic >>

Re: method parameters

by Mark Space <markspace@[EMAIL PROTECTED] > May 11, 2008 at 01:38 PM

david wrote:
> Yeah, I just did some research about this issue and find the same as
> you suggested.
> This is my second program with the JAVA, I need to use it to write
> several programs that would be cross-platform. And at the same time I
> reading one small book, it will take several weeks before I will be
> able to understand the idea of the JAVA better. But I it looks that I
> capable of writing JAVA code, just maybe it's abit too much objective,
> that means you need to know more about the standard cl*****.

The basic idiom for emulating pass by reference is to wrap the value in 
an array.

   public void someMethod( int w[] )
   {
     w[0] = 42;
   }

will let you set v to 42 when called like this:

   int v = 0;
   someMethod( new int[] {v} );

But it's very awkward because you already have an array. (It's even 
pretty awkward when you don't have an array.)  A better method might be 
to do as Lew suggests and just wrap your array in a class

public class BoolMatrix {
   private boolean m [][];
   public void resetSize( int x, int y ) {
      m = new boolean[x][y];
   }
}

Now things will work much more like you'd expect.

   BoolMatrix n = new BoolMatrix();
   int size = 42;
   someOtherMethod( n, size );

  // ...
   public void someOtherMethod( BoolMatrix x, int size )
   {
     x.resetSize( size, size );
   }

This is much clearer to read and debug, I think. Few surprises here, at 
least until you get into multiple threads... ;)
 




 7 Posts in Topic:
method parameters
david <David.Abdurachm  2008-05-11 11:22:05 
Re: method parameters
Eric Sosman <esosman@[  2008-05-11 14:34:33 
Re: method parameters
david <David.Abdurachm  2008-05-11 11:46:27 
Re: method parameters
Lew <lew@[EMAIL PROTEC  2008-05-11 14:54:21 
Re: method parameters
Patricia Shanahan <pat  2008-05-11 12:04:45 
Re: method parameters
david <David.Abdurachm  2008-05-11 12:13:54 
Re: method parameters
Mark Space <markspace@  2008-05-11 13:38:47 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 20:52:21 CDT 2008.