Home » Java programming language

Java Class class getDeclaredMethod() method with example

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

Class class getDeclaredMethod() method

  • getDeclaredMethod() method is available in java.lang package.
  • getDeclaredMethod() method is used to return Method objects that indicate the given declared method of the class or an interface denoted by this Class object.
  • getDeclaredMethod() method is a non-static method, it is accessible with the class objects only and if we try to access the method with the class name then we will get an error.
  • getDeclaredMethod() method accepts two arguments, the first argument is of "String" type and the second argument is an array of "Class" type.
  • getDeclaredMethod() method may throw an exception at the time of returning a Method object.
    • NoSuchMethodException: In this exception when a specifying method does not exist.
    • SecurityException: In this exception, it may raise when the security manager exists.
    • NullPointerException: In this exception when the given Method name is null.


    public Method getDeclaredMethod (String method_name, Class ...paramType);


  • String method_name – represents the name of the method.
  • Class ...paramType – represents the parameter array of "Class" type.

Return value:

The return type of this method is Method, it returns the Method object for the method of this Class meets the given method_name and parameter array paramType.


// Java program to demonstrate the example of Method 
// getDeclaredMethod (String method_name, Class ...paramType)
// method of Class 

import java.lang.reflect.*;

public class GetDeclaredMethodOfClass {
    public static void main(String[] args) throws Exception {
        String str = new String();
        GetDeclaredMethodOfClass dc = new GetDeclaredMethodOfClass();

        // Get Class object of String
        Class cl = str.getClass();

        // Get Class object of GetDeclaredMethodOfClass
        Class dm = dc.getClass();

        // Calling No argument Method
        Method no_argument_method = cl.getDeclaredMethod("length", null);
        System.out.println(" String Method = " + no_argument_method.toString());

        Class[] method_arguments = new Class[2];
        method_arguments[0] = Integer.class;
        method_arguments[1] = Float.class;

        // Calling argument Method
        Method argument_method = dm.getDeclaredMethod("argumentMethod", method_arguments);
        System.out.println("This Class Method = " + argument_method.toString());

    public void argumentMethod(Integer i, Float f) {
        this.i = i;
        this.f = f;

    public int i = 10;
    private float f = 10.2f;


String Method = public int java.lang.String.length()
This Class Method = public void GetDeclaredMethodOfClass.argumentMethod(java.lang.Integer,java.lang.Float)


Comments and Discussions



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

© some rights reserved.