Java Class isSealed() Method

The isSealed() method in Java, part of the java.lang.Class class, is used to determine whether the class object represents a sealed class.

Table of Contents

  1. Introduction
  2. isSealed() Method Syntax
  3. Understanding isSealed()
  4. Examples
    • Basic Usage
    • Checking Non-Sealed Classes
  5. Real-World Use Case
  6. Conclusion

Introduction

The isSealed() method returns true if the class object represents a sealed class, otherwise it returns false. This method is useful for reflection-based operations where you need to verify if a class is sealed. Sealed classes, introduced in Java 17, allow developers to define a restricted hierarchy of classes.

isSealed() Method Syntax

The syntax for the isSealed() method is as follows:

public boolean isSealed()

Parameters:

  • This method does not take any parameters.

Returns:

  • true if this class object represents a sealed class; false otherwise.

Understanding isSealed()

The isSealed() method checks whether the class object represents a sealed class. Sealed classes restrict which other classes or interfaces can extend or implement them. This is useful for creating a more controlled and predictable class hierarchy.

Examples

Basic Usage

To demonstrate the basic usage of isSealed(), we will create a sealed class and check if it is sealed.

Example

public class IsSealedExample {
    public static void main(String[] args) {
        Class<?> animalClass = Animal.class;
        boolean isSealed = animalClass.isSealed();

        System.out.println("Is Animal a sealed class? " + isSealed);
    }

    public sealed class Animal permits Dog, Cat {}

    public final class Dog extends Animal {}

    public final class Cat extends Animal {}
}

Output:

Is Animal a sealed class? true

Checking Non-Sealed Classes

This example shows how the isSealed() method behaves with non-sealed classes.

Example

public class NonSealedExample {
    public static void main(String[] args) {
        Class<String> stringClass = String.class;
        boolean isSealed = stringClass.isSealed();

        System.out.println("Is String a sealed class? " + isSealed);
    }
}

Output:

Is String a sealed class? false

Real-World Use Case

Dynamic Sealed Class Checking in Frameworks

In a real-world scenario, you might use the isSealed() method to dynamically check for sealed classes within a framework. This can be useful for operations such as serialization, deserialization, or custom processing logic that needs to handle sealed classes differently.

Example

import java.lang.reflect.Modifier;

public class SealedClassChecker {
    public static void main(String[] args) {
        checkIfSealed(Animal.class);
        checkIfSealed(String.class);
    }

    public static void checkIfSealed(Class<?> clazz) {
        if (clazz.isSealed()) {
            System.out.println(clazz.getName() + " is a sealed class.");
        } else {
            System.out.println(clazz.getName() + " is not a sealed class.");
        }
    }

    public sealed class Animal permits Dog, Cat {}

    public final class Dog extends Animal {}

    public final class Cat extends Animal {}
}

Output:

SealedClassChecker$Animal is a sealed class.
java.lang.String is not a sealed class.

Conclusion

The Class.isSealed() method in Java provides a way to determine whether a class object represents a sealed class. By using this method, you can dynamically check and process sealed classes, making it particularly useful for reflection-based operations in frameworks and libraries.

Whether you are working with standard classes or custom sealed classes, the isSealed() method offers a reliable way to verify sealed class types at runtime.

Comments