Var arg method


Example of Var Arg Method

package MethodOverloading;

public class Varmethod
{
   void add(int...args)
   {
     int sum=0;
     for(int i=0;i<args.length;i++)
      {
        sum=sum+args[i];
      }
      
     System.out.println(sum);
   }

    void add(int a)
     {
       System.out.println("runnning int arg");
     }
     void add(int a,int b)
     {
       System.out.println("multi arg method");
     }
     public static void main(String[] args)
      {
         Varmethod v=new Varmethod();
          v.add(5);// int arg method called
          v.add(5,7);//two int arg method called
          v.add(2,2,3,4);// var arg method called
   }
}

OUTPUT:
runnning int arg
multi arg method
11

Leave a Reply