Skip to content
Related Articles

Related Articles

Method Class | getDefaultValue() Method in Java
  • Last Updated : 05 Dec, 2018

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.




/*
* 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();
    }
    }
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.




/*
* 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;
    }
}
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–

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :