java.lang.reflect.Method.getDefaultValue() 方法示例

描述

java.lang.reflect.Method.getDefaultValue() 方法返回此方法实例所表示的注释成员的默认值。如果成员是原始类型,则返回相应包装器类型的实例。如果成员没有关联的默认值,或者方法实例不代表注释类型的声明成员,则返回 null。

声明

以下是 java.lang.reflect.Method.getDefaultValue() 方法的声明。

public Object getDefaultValue()

返回

此方法实例所表示的注释成员的默认值。

异常

TypeNotPresentException − 如果注释类型为 Class,并且找不到默认类值的定义。

示例

以下示例显示了 java.lang.reflect.Method.getDefaultValue() 方法的用法。

package com.tutorialspoint;

import java.lang.reflect.Method;

public class MethodDemo {

   public static void main(String[] args) {

      Method[] methods = SampleClass.class.getMethods();
      System.out.println(methods[0].getDefaultValue());
   }
}

class SampleClass {
   private String sampleField;

   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField;
   }
}

让我们编译并运行上述程序,这将产生以下结果 −

null

java_reflect_method.html