Method Class | getDefaultValue() Method in Java

The getDefaultValue() method of java.lang.reflect.Method class. It returns the default value for the annotation member represented by the Method object.
When annotation member belongs to primitive types, then the method returns the instance of that wrapper type. It returns null if annotation member does not contain any default value, or if the method object does not have a member of an annotation type.

Syntax:

public Object getDefaultValue()

Return Value: This method returns the default value for the annotation member represented by this Method object.

Exception: This method throws a TypeNotPresentException if the annotation is of type Class and no definition can be found for the default class value.

Below program illustrates getDefaultValue() method of Method class:

Example 1: Below Program contains a interface name demo with some default value for the annotation member represented by methods of interface.The Main method of program creates Method Object for every method of Interface and print the default value for the annotation if method contains such default value for the annotation.

filter_none

edit
close

play_arrow

link
brightness_4
code

/*
* Program Demonstrate getDefaultValue() method 
* of Method Class.
*/
import java.lang.reflect.Method;
  
// Main Class
public class GFG {
    public static void main(String[] args)
        throws NoSuchMethodException, SecurityException
    {
  
        // Get Method Object
        Method methodObj1 = demo
                                .class
                                .getDeclaredMethod("fauvMovie");
  
        // Apply getDefaultValue() method
        Object obj1 = methodObj1.getDefaultValue();
  
        // print default value
        System.out.println(obj1);
  
        // Get Method Object
        Method methodObj2 = demo
                                .class
                                .getDeclaredMethod("fauvMovieRating");
  
        // Apply getDefaultValue() method
        Object obj2 = methodObj2.getDefaultValue();
  
        // print default value
        System.out.println(obj2);
  
        // Get Method Object
        Method methodObj3 = demo
                                .class
                                .getDeclaredMethod("fauvMovieReleaseYear");
  
        // Apply getDefaultValue() method
        Object obj3 = methodObj3.getDefaultValue();
  
        // print default value
        System.out.println(obj3);
    }
  
    // an interface with default annotations
    public @interface demo {
        public String fauvMovie() default "Inception";
        public double fauvMovieRating() default 9.6;
        public int fauvMovieReleaseYear();
    }
    }

chevron_right


Output:

Inception
9.6
null

Example 2: Below Program contains a interface name demo with some default value for the annotation member represented by methods of interface and a class name demo with no such default value for the annotation .The Main method of program creates Method Object for every method of Interface and class and print the default value for the annotation if method contains such default value for the annotation.

filter_none

edit
close

play_arrow

link
brightness_4
code

/*
* Program Demonstrate getDefaultValue() method 
* of Method Class.
*/
import java.lang.annotation.*;
import java.lang.reflect.Method;
  
// a simple class with no annotations
class demo {
    public void demoMethod(String value)
    {
        System.out.println("demo method: " + value);
    }
}
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
        try {
            // create class object
            Class classobj = demo.class;
            Class[] parameterTypes = { String.class };
  
            // get Method Object
            @SuppressWarnings("unchecked")
            Method method = classobj
                                .getMethod("demoMethod",
                                           parameterTypes);
  
            // Print default value
            System.out.println("default value of demoMethod():"
                               + method.getDefaultValue());
  
            // get Method Object
            Method methodobj = demoInterface
                                   .class
                                   .getDeclaredMethod("getValue");
  
            // get default value
            Object defaultValue = methodobj.getDefaultValue();
  
            // Print default value
            System.out.println("default value of getValue():"
                               + defaultValue);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    // a interface with some annotation
    @Target({ ElementType.METHOD })
    @Retention(RetentionPolicy.RUNTIME)
    private @interface demoInterface {
        int getValue() default 51;
    }
}

chevron_right


Output:

default value of demoMethod():null
default value of getValue():51

Reference:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getDefaultValue–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.