Switch to full style
General Java code examples
Post a reply

method with a variable number of parameters

Fri Dec 12, 2008 9:56 pm

In java J2SE0.5 you can now make a function with a variable number of paramters. The rules to do that is :

- Only varaible paramter list only .
- varaible paramter list must apper in the last inside the parentheses of the function.
- defined as : type followed by three dots and the name .

And example about method with a variable number of parameters.
Code:
import java.io.*;
class MyClass {
public void printStuff(String greet, int... values) {
for (int v : values ) {
System.out.println( greet + ":" + v);
}
}
}
class VarargTest {
public static void main(String[] args) {
MyClass mc = new MyClass();
mc.printStuff("Hello", 1);
mc.printStuff("Hey", 1,2);
mc.printStuff("Hey you", 1,2,3);
}
}

/* code taken from apress SCJP 1.5 */




Post a reply
  Related Posts  to : method with a variable number of parameters
 Method Parameters,,,is it by value or reference???     -  
 pass parameters to main method args[] variables     -  
 different between Local variable and Global variable     -  
 convert integer number to octal,hexadecimal number systems     -  
 convert octal number to decimal number     -  
 convert decimal number to octal number     -  
 Unset variable     -  
 Variable interpolation     -  
 PHP variable in javascript     -  
 transient variable     -  

Topic Tags

Java Methods