Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

When working with Java programming, there are situations where we may need to pass a class as a parameter, enabling dynamic behavior and flexibility in our code.

In this tutorial, we’ll delve into different approaches for achieving this in Java.

2. Problem Definition

In Java, classes serve as blueprints for objects, defining their properties and behaviors. Moreover, passing a class as a parameter entails providing a reference to the class itself rather than an instance of the class.

This allows methods to operate on or instantiate objects of different classes dynamically.

3. Passing a Class Parameter

We can pass a class as a parameter just like any other object. Besides, we can use the Class class to represent classes in Java and pass instances of this class as arguments to methods. Here’s a simple implementation:

public class Example {
    public static void processClass(Class<?> clazz) {
        System.out.println("Processing class: " + clazz.getName());
    }

    public static void main(String[] args) {
        processClass(String.class);
        processClass(Integer.class);
        processClass(Double.class);
    }
}

In the above example, the processClass() method accepts a Class<?> parameter, which represents a class of any type. Hence, we pass different classes (String.class, Integer.class, Double.class) to demonstrate the flexibility of this approach.

4. Reflection

One common application of passing a class as a parameter is in reflection, where classes are dynamically loaded and instantiated at runtime. Moreover, reflection allows us to inspect classes, methods, and fields and invoke methods at runtime. Here’s a simple example:

public class ReflectionExample {
    public static void processClass(Class<?> clazz, String methodName) throws Exception {
        Method method = clazz.getMethod(methodName);
        Object instance = clazz.getDeclaredConstructor().newInstance();
        method.invoke(instance);
    }

    public static void main(String[] args) throws Exception {
        processClass(ReflectionTarget.class, "sayHello");
    }
}

class ReflectionTarget {
    public void sayHello() {
        System.out.println("Hello, Reflection!");
    }
}

In this example, the invokeMethod() method takes a clazz class and a methodName as parameters. It uses reflection to dynamically invoke the specified method sayHello() on the provided class ReflectionTarget.

5. Using Generics

Another technique involves leveraging generics to pass classes as parameters, offering type safety and flexibility. Generics allow methods to operate on a wide range of class types, enhancing code reusability and readability. Here’s an example:

public class GenericExample {
    public static <T> void printListElements(Class<T> clazz, List<T> list) {
        System.out.println("Elements of " + clazz.getSimpleName() + " list:");
        for (T element : list) {
            System.out.println(element);
        }
    }

    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("Java");
        stringList.add("is");
        stringList.add("awesome");

        printListElements(String.class, stringList);
    }
}

In this example, the printListElements() method is a generic method that takes a class clazz and a list of elements list as parameters. It prints the elements of the list along with the class name. This allows the method to work with lists of different types.

6. Conclusion

In conclusion, passing a class as a parameter in Java is a versatile technique that enables dynamic behavior, reflection, and generic programming. By accepting classes as arguments, methods become more flexible, reusable, and adaptable to different scenarios.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW