user663724
user663724

Reputation:

How to pass an An Object array to a method

I have a Method called as getStrategy inside a class and its expecting an array as shown .

public static String getStrategy(Hand[] Hand)
{

}

And this is my Hand Object

public class Hand
{
  public char side;
}

On to my client , Please tell me how can i pass this getStrategy method ??

I have tried this

Hand[] HandArray = new Hand[1];

HandArray.side = 's';

MyClassUtil.getStrategy(HandArray);

Please tell me if this si correct or not ??

Upvotes: 0

Views: 136

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499800

Well all but the middle line is fine (naming aside). Currently your middle line is trying to access an array as if it were a single object. You probably want something like:

Hand[] hands = new Hand[1];
Hand hand = new Hand();
hand.side = 's';

hands[0] = hand;
MyClassUtil.getStrategy(hands);

I would also strongly suggest not using public fields...

Upvotes: 5

Related Questions