Java Class.getEnclosingClass()

Syntax

Class.getEnclosingClass() has the following syntax.

public Class <?> getEnclosingClass()

Example

In the following code shows how to use Class.getEnclosingClass() method.


public class Main {
  public static void main(String[] args) {
    Main cls = new Main();
  }//from w  ww. j av  a2s.c  o  m
}

class MyClass {
  public MyClass() {
    class Outer {
      public void show() {
        class Inner {
          public void show() {
            System.out.print(getClass().getName() + " inner in...");
            System.out.println(getClass().getEnclosingClass());
          }
        }
        System.out.print(getClass().getName() + " inner in...");
        System.out.println(getClass().getEnclosingClass());
        Inner i = new Inner();
        i.show();
      }
    }
    Outer o = new Outer();
    o.show();
  }
}




















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable