Java.lang.Class.getEnclosingMethod() Method

Advertisements


Description

The java.lang.Class.getEnclosingMethod() returns a Method object representing the immediately enclosing method of the underlying class, if this Class object represents a local or anonymous class within a method, else returns null.

Declaration

Following is the declaration for java.lang.Class.getEnclosingMethod() method

public Method getEnclosingMethod()

Parameters

  • NA

Return Value

This method returns the immediately enclosing method of the underlying class, if that class is a local or anonymous class; else null.

Exception

  • NA

Example

The following example shows the usage of java.lang.Class.getEnclosingMethod() method.

package com.tutorialspoint;

import java.lang.*;

public class ClassDemo {

   public Object c;

   public ClassDemo() {
      class ClassA{};
      c = new ClassA();
   }

   public Object ClassAObject() {
      class ClassA{};
      return new ClassA( );
   }

   public static void main(String[] args) {
     
     Class cls;
     cls = (new ClassDemo()).ClassAObject().getClass();
         
     System.out.print("Method = ");
     System.out.println(cls.getEnclosingMethod());
   }
} 

Let us compile and run the above program, this will produce the following result:

Method = public java.lang.Object ClassDemo.ClassAObject()



Advertisements
Advertisements