Monday, December 15, 2025
HomeLanguagesJavaMethod Class | isVarArgs() Method in Java

Method Class | isVarArgs() Method in Java

The java.lang.reflect.Method.isVarArgs() method of Method class checks whether Method Object was declared to take a variable number of arguments or not. If the method can take a variable number of arguments then returns true otherwise it will return false. VarArgs(variable-length arguments): VarArgs allows method to accept a number of arguments. It is a better approach to pass arguments than array when it is not known how many arguments to pass in method. 

Syntax:

public boolean isVarArgs()

Return Value: This method returns true if and only if Method has variable-length arguments else false. 

Below programs illustrates isVarArgs() method of Method class: 

Example 1: Below program checks GFG class methods whether Method has variable-length arguments or not. In this program a method accepts VarArgs and by isVarArgs() check method accept VarArgs or not and at last print the result. 

Java




// Program Demonstrate isVarArgs() method
// of Method Class.
 
import java.lang.reflect.Method;
 
public class GFG {
 
    // create another method
    public static void paint(Object... values)
    {
        String message = "A Computer Science portal for geeks";
    }
 
    // create main method
    public static void main(String args[])
    {
 
        try {
 
            // get list of declared method objects of class GFG
            Method[] methods = GFG.class.getMethods();
 
            // loop through method list
            for (Method method : methods) {
 
                // check method contains VarArgs or not
                boolean isVarArgs = method.isVarArgs();
 
                // print result if VarArgs are present
                if (isVarArgs)
                    System.out.println(method + " method accepts VarArgs :"
                                       + isVarArgs);
            }
        }
        catch (Exception e) {
 
            // Print Exception if any Exception occurs
            e.printStackTrace();
        }
    }
}


Output:

public static void GFG.paint(java.lang.Object[]) method accepts VarArgs :true

Example 2: This program is going to return all the Methods which contains variable-length arguments of class java.util.Collections. Explanation: In this Method at first java.util.Collections Class Object is created. After creating Class Object of java.util.Collections Class a list of Method Objects is created by calling getMethods() of class Object. Iterate through Method list and get Method contains variable-length arguments using isVarArgs(). At last print Synthetic Method name. 

Java




// Program Demonstrate isVarArgs() method
// of Method Class.
 
import java.lang.reflect.Method;
import java.util.Collections;
public class GFG {
 
    // create main method
    public static void main(String args[])
    {
 
        try {
 
            // create class object for class Collections
            Class c = Collections.class;
 
            // get list of Method object
            Method[] methods = c.getMethods();
 
            System.out.println("Methods of Collections Class"
                               + " contains VarArgs");
            // Loop through Methods list
            for (Method m : methods) {
 
                // check whether the method  contains VarArgs or not
                if (m.isVarArgs())
                    // Print Method name
                    System.out.println("Method: " + m.getName());
            }
        }
        catch (Exception e) {
            // print Exception if any Exception occurs
            e.printStackTrace();
        }
    }
}


Output:

Methods of Collections Class contains VarArgs
Method: addAll

Reference:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32448 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6817 POSTS0 COMMENTS
Nicole Veronica
11954 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6955 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6899 POSTS0 COMMENTS
Umr Jansen
6884 POSTS0 COMMENTS