Java Methods parameter lists.

Java Methods variable-length parameter lists

  • When you specify a variable-length parameter for a method, you must use ... (Three dots) after the parameter type and then the variable name.
    Variable-Length parameter example:
    public class VariableLength {
    void printObjects( Object ... list ) {
       // list is an Object [] (array)
       for( Object o : list )
           System.out.println( o );
    }
      public static void main(String args[]) {
         String[] priceCat = {"Low", "Middle", "High"};
         new VariableLength().printObjects(priceCat);
      }
    }
    The result of this is:
    Low
    Middle
    High
    You can download this example here (needed tools can be found in the right menu on this page).
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.