Export (0) Print
Expand All

java.lang.Class.getDeclaredField(String fieldName)

Visual Studio 2005

Gets the field (public, private, or protected) with the given name.


public java.lang.reflect.Field getDeclaredField(java.lang.String fieldName)

Parameters

Parameter Description

fieldName

The name of the field. A abfe0115-00b8-44a6-b2f8-927aa2a8cbcc is thrown if there is no field with the given name.

A 3d033269-e08f-4bd9-b84e-9d75a0cddd44 object representing the field of the given name.

// class_getdeclaredfield.jsl

import java.lang.reflect.Field;
import java.util.Date;

public class Program
{
    public static void main(String[] args)
    {
        try
        {
            // Get the Class object associated with this class.
            Program program = new Program();
            Class progClass = program.getClass();

            // Get the field named str.
            Field strField = progClass.getDeclaredField("str");
            System.out.println("Field found: " + strField.toString());

            // Get the field named date.
            Field dateField = progClass.getDeclaredField("date");
            System.out.println("Field found: " + dateField.toString());

            // Get the field named i.
            Field iField = progClass.getDeclaredField("i");
            System.out.println("Field found: " + iField.toString());
        }
        catch (NoSuchFieldException ex)
        {
            System.out.println(ex.toString());
        }
    }

    public Program()
    {
    }

    public Program(String str, Date date, int i)
    {
        this.str = str;
        this.date = date;
        this.i = i;
    }

    public String str = "Hello";
    private Date date = new Date();
    protected int i = 0;
}

/*
Output:
Field found: public java.lang.String Program.str
Field found: private java.util.Date Program.date
Field found: protected int Program.i
*/

Community Additions

ADD
Show:
© 2014 Microsoft