1

I want to use a method in java. its prototype is defined like below:

 public void fragmentRequestAction(Fragment fragment, int requestId, Object... objects)

I do not know what is Object... how can I pass such items to the method and how can I use them?

Husein Behboudi Rad
  • 5,434
  • 11
  • 57
  • 115

1 Answers1

0

The Object... takes any non-primitive type and at any number. In Java it is called variable length argument.

It means you may call the fragmentRequestAction() method like this -

fragmentRequestAction(fragment, 345); //no object here
fragmentRequestAction(fragment, 345, someObj);
fragmentRequestAction(fragment, 345, someObj1, someObj2);
fragmentRequestAction(fragment, 345, someObj1, someObj2, someObj3);  

Variable length argument intruded from java 5. There is some rules to remember while constructing function with variable length arguments. See the code snippet -

public void meth ( int... a) // valid
public void meth (double a, int... b) // valid
public void meth ( int... a, int b) // invalid- Ellipsis may be used towards the end only
public void meth ( int... a, double... b) // invalid - More than one variable length parameter list may not be used
public void meth ( Student... a) // valid - Reference types are also allowed
public void meth( int[]... a) // valid - reference types are also allowed 

Visit the link for more details.

Razib
  • 10,965
  • 11
  • 53
  • 80