Monday, March 5, 2018

VarArgs - Variable Argument

Can be add more method variable without initialize.

return_type  method_name(DataType... variableName){}


class A{
    public static void varArgsMethod(String... value){
       sysout("Hi");
   }
    psvm(){
       A.varArgsMethod("Welcome","Var","args"); //Can add zero or more argument
   }
}

Output - Hi

Rules

  • Only can be one Variable argument in method

             ex - public void run(String... walk, int... time){}; //Compile error


  • Variable Argument must be last

            ex - public void  walk(int x, int... y){}




No comments:

Post a Comment