Constructor isVarArgs() method in Java with Examples

The isVarArgs() method of java.lang.reflect.Constructor class is used to return the boolean value true if this Constructor can take a variable number of arguments as parameters else method will return false.VarArgs allows the constructor to accept a number of arguments. using VarArgs is a better approach to pass arguments than array when it is not known how many arguments to pass in the constructor.

Syntax:

public boolean isVarArgs()

Parameters: This method accepts nothing.

Return: This method returns true if and only if this executable was declared to take a variable number of arguments.

Below programs illustrate isVarArgs() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate isVarArgs() method
  
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = shape.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        // check isVargs or not
        boolean isVargs = cons[0].isVarArgs();
  
        // print result
        System.out.println("isVargs : " + isVargs);
    }
  
    public class shape {
  
        public shape(Object... objects)
        {
        }
    }
}

chevron_right


Output:

isVargs : true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate isVarArgs() method
  
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = String.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        for (int i = 0; i < cons.length; i++) {
            // check isVargs or not
            boolean isVargs = cons[i].isVarArgs();
  
            // print result
            System.out.println(cons[i]);
            System.out.println("isVargs : " + isVargs);
        }
    }
}

chevron_right


Output:

public java.lang.String(byte[], int, int)
isVargs : false
public java.lang.String(byte[], java.nio.charset.Charset)
isVargs : false
public java.lang.String(byte[], java.lang.String) throws java.io.UnsupportedEncodingException
isVargs : false
public java.lang.String(byte[], int, int, java.nio.charset.Charset)
isVargs : false
public java.lang.String(byte[], int, int, java.lang.String) throws java.io.UnsupportedEncodingException
isVargs : false
public java.lang.String(java.lang.StringBuilder)
isVargs : false
public java.lang.String(java.lang.StringBuffer)
isVargs : false
public java.lang.String(byte[])
isVargs : false
public java.lang.String(int[], int, int)
isVargs : false
public java.lang.String()
isVargs : false
public java.lang.String(char[])
isVargs : false
public java.lang.String(java.lang.String)
isVargs : false
public java.lang.String(char[], int, int)
isVargs : false
public java.lang.String(byte[], int)
isVargs : false
public java.lang.String(byte[], int, int, int)
isVargs : false

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#isVarArgs()

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.