Java Program to Check if a given Class is an Anonymous Class


The type of nested inner class that has no name is called as anonymous class. Before making a java program to check if a given class is an anonymous class we need to understand the concept of nested class. Let’s discuss this in detail.

Nested Class

When we create class within another class, it is referred as nested class. The nested classes share the same scope. It allows the grouping of similar classes and it increases the encapsulation (encapsulation means wrapping of similar functionalities in a single unit). In java, we know that a class could not be defined with private access specifier but we can define nested class as private. There are two types of nested class available: static and non-static nested class.

The static nested class is defined using static modifier but Non-static nested class is not defined using static keyword and it is also known as the inner class.

Inner Class

Non-static or Inner class can access all the static and non-static methods and member variables of its outer class. It can also access the members that are private but, an outer class cannot access the private members of inner class. Inner class is the most used type of nested class. We will discuss its type later in this article.

Syntax

class Class_otr {
   // Outer class body
   class Class_inn { 
      // Inner class body
   }
} 

To create class we have used ‘class’ keyword. ‘Class_otr’ is the name of outer class and ‘Class_inn’ is the name of inner class.

Example

The following program illustrates the inner class.

Example

class Cart {  
   // Outer class
   private String item1 = "Rice";
   private class Bill {   
      public void info() {
         System.out.println("This is an inner class");
         // accessing private member ‘item1’
         System.out.println("Item 1 in Cart is: " + item1);
      }
   }
   void display() {
      Bill ob = new Bill(); 
      // creating object of class Bill
      ob.info(); 
      // method calling using object ‘ob’
   }
}
public class Main {
   public static void main(String args[]) {
      Cart obj = new Cart(); 
      // creating object of class Cart
      obj.display(); 
      // calling method of outer class Cart using its object
   }
}

Output

This is an inner class
Item 1 in Cart is: Rice

In the above program, the class ‘Cart’ is outer class of ‘Bill’. We have created a method ‘info’ that can access the private member ‘item1’ of outer class. Notice that ‘Bill’ is a private class and we have created its object inside the method ‘display()’ of outer class. The object of ‘Cart’ is created inside the main method. When we called the ‘display()’ method it has automatically called the members of class ‘Bill’. Hence we got that output.

Program to Check if a given class is an Anonymous Class

There are two main types of inner class −

  • Local Inner Class − It is defined inside a method block. We can also define it in the body of loops. Just like the local variables and methods, its scope lies within the method or loop block where it is defined.

  • Anonymous Inner Class − It is the type of inner class that has no name. Its declaration and initialization happens at the same time. It is mainly used for overriding methods.

    We can use abstract keywowrd to declare anonymous class.

Syntax

Class_name object_name = new Class_name() {
   return_type method_name(){
      // code to be executed
   }
};

We will use the following inbuilt methods of java to check if a given class is anonymous or not −

  • getClass() − It returns the class of specified object.

  • isAnonymousClass() − It returns true if a given class is anonymous otherwise false.

Example

import java.lang.*;
abstract class Anonymous { 
   // abstract class
   abstract public void display(); 
   // abstract method
}
public class Main {
   public static void main(String args[]) {
      Anonymous obj = new Anonymous() { 
         // anonymous inner class
         public void display() { 
            // overriding display() method
            System.out.println("I am display method of anonymous inner class");
         }
      };
      obj.display(); 
      // calling display() method
      // fetching the name of class of object ‘obj’ i.e. Anonymous
      Class cls = obj.getClass(); 
      // Checking if class ‘Anonymous’ is anonymous class or not
      boolean isAnonymous = cls.isAnonymousClass();
      // Printing the Boolean result
      System.out.println("Is Anonymous a Anonymous class: " + isAnonymous);
   }
}

Output

I am display method of anonymous inner class
Is Anonymous a Anonymous class: true

Conclusion

In this article, we have discussed a java program to check if a given class is an anonymous class. In our program we have used different inbuilt methods of java.lang package. We have also understood the concept of nested and inner classes in java.

Updated on: 02-May-2023

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements