Home » Java programming language

Java Class class getDeclaredFields() method with example

Class class getDeclaredFields() method: Here, we are going to learn about the getDeclaredFields() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 13, 2019

Class class getDeclaredFields() method

  • getDeclaredFields() method is available in java.lang package.
  • getDeclaredFields() method is used to return an array of Field object that indicate all the Fields (i.e. whether it is private, public, protected, or default) but it does not include inherited fields.
  • getDeclaredFields() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getDeclaredFields() method may throw an exception at the time of returning an array of Field object.
    SecurityException: In this exception it may raise when security manager exists.

Syntax:

    public Field[] getDeclaredFields();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Field[], it returns an array of Field object denoting all the declared field of this class excluding inherited Fields.

Note:

  • When class or interface does not contain field, 0 is returned.
  • When this class object holds primitive, an array class, or void type, 0 is returned.

Example:

// Java program to demonstrate the example 
// of Field[] getDeclaredFields() method of Class 

import java.lang.reflect.*;

public class GetDeclaredFieldsOfClass {
    public static void main(String[] args) throws Exception {
        GetDeclaredFieldsOfClass declare_field = new GetDeclaredFieldsOfClass();

        // Get Class
        Class cl = declare_field.getClass();

        // It returns an array of Field objects that indicate
        // any field(private,public,protected etc) represented by the 
        // Class excluding inherited fields 
        Field[] f = cl.getDeclaredFields();

        for (int i = 0; i < f.length; ++i)
            System.out.println("Declared Fields: " + f[i].toString());
    }

    // Private Constructors
    private GetDeclaredFieldsOfClass() {
        System.out.println("We are in private constructor");
    }

    // Public Constructors
    public GetDeclaredFieldsOfClass(int i, short s) {
        this.i = i;
        this.s = s;
    }

    private int i = 100;
    public short s = 200;
}

Output

We are in private constructor
Declared Fields: private int GetDeclaredFieldsOfClass.i
Declared Fields: public short GetDeclaredFieldsOfClass.s






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.