java.lang.Class.getDeclaredField()方法

java.lang.Class.getDeclaredField()方法

java.lang.Class.getDeclaredField()方法用法实例教程 - 方法返回一个Field对象,它反映此Class对象所表示的类或接口的指定已声明字段。

描述

java.lang.Class.getDeclaredField()方法返回一个Field对象,它反映此Class对象所表示的类或接口的指定已声明字段。name参数是一个字符串,指定所需的字段的简单名称。

声明

以下是声明java.lang.Class.getDeclaredField()方法

publicField getDeclaredField(String name)throwsNoSuchFieldException,SecurityException

参数

·        name -- This is the name of the field.

返回值

此方法返回这个类中的指定字段的Field对象。

异常

·        NoSuchFieldException -- If a field withthe specified name is not found.

·        NullPointerException -- 如果name null.

·        SecurityException -- If a security manager, s, ispresent.

实例

下面的例子说明了如何使用java.lang.Class.getDeclaredField()方法。

package com.yiibai;
 
import java.lang.reflect.*;
 
public class ClassDemo {
 
   public static void main(String[] args) {
 
     try {            
        ClassDemo c = new ClassDemo();
        Class cls = c.getClass();
 
        // field long l
        Field lVal  = cls.getDeclaredField("l");
        System.out.println("Field = " + lVal.toString());
     }
     catch(Exception e) {
        System.out.println(e.toString());
     }
   }
 
   public ClassDemo() {
      // no argument constructor
   }
 
   public ClassDemo(long l) {
      this.l = l;
   }
 
   long l = 77688;
}

让我们来编译和运行上面的程序,这将产生以下结果:

Field=longClassDemo.l

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值