---
schema_version: "1"
id: 0a0f831346ecd8ef511249612ae22f9d9684519f4cae102709f1e73e0ae2ad86
title: Test for an Instance of a Class
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/java-module/latest/java-instanceof.md
canonical_url: https://docs.mulesoft.com/java-module/latest/java-instanceof
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 5e14fa524a5f675b149a2b92318d4be2aa2206a1bf12c542bfcedaad4d10b4c2
description: MuleSoft Documentation Site
release: "2.0"
latest: true
---
---
product: Java Module
version: 2.0
is-latest-version: true
---

# Test for an Instance of a Class

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The Java module provides a `Validate type` operation that tests whether an object is an instance of
a class. This operation can also be configured to accept or reject subtypes of a class. If `true`,
the operation does not modify the payload and the flow continues its execution. Otherwise, the
operation throws an error, interrupting the flow, and passes control to the
[error handler](https://docs.mulesoft.com/mule-runtime/latest/mule-error-concept#error-handlers).

## Validate Type Operation

This example defines the `Person` class:

```java
package com.me;

public class Person {

    private String name;
    private Integer age;

    public Person() {
        this.name = "Max Mule";
        this.age = 10;
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}
```

This example defines the `Employee` class that extends the `Person` class:

```java
package com.me;

public class Employee extends Person {

    private String company;

    public Employee(String name, Integer age, String company) {
        super(name, age);
        this.company = company;
    }
}
```

> [!IMPORTANT] The classes used with the Java module must be exported (unless they belong to the
> default package). If not, the execution fails with a JAVA:CLASS\_NOT\_FOUND error. See
> [how to export resources](https://docs.mulesoft.com/mule-runtime/latest/how-to-export-resources).

To check whether an object belongs to a certain class, an instance of the object is needed. This can
be done by calling the [`new` operation](https://docs.mulesoft.com/java-module/latest/java-create-instance) to create an `Employee` object and
place it in the `jane` variable:

```xml
<java:new class="com.me.Employee"
    constructor="Employee(String, Integer, String)"
    target="jane">
    <java:args>#[{
      name: 'Jane Avi',
      age: 28,
      company: MuleSoft
    }]</java:args>
</java:new>
```

To check if the object stored in `vars.jane` belongs to the `Employee` class, the `Validate type`
operation can be used:

```xml
<java:validate-type class="com.me.Employee" instance="#[vars.jane]" />
```

In this case, the object is an instance of `Employee`, so the flow continues executing normally, and
the payload is not modified.

### Check Against Subtypes (Inheritance)

The `Validate type` operation has the `Accept subtypes` parameter, which indicates if the operation
should accept all subclasses of a class. This parameter can be `true`, `false`, or an expression,
and it defaults to `true` if omitted.

Continuing with the previous example, the object in `vars.jane` belongs to the `Employee` class
which extends the `Person` class, that is, `Employee` is a subtype of `Person`. So executing this
example resolves to `true` and the flow executes normally.

```xml
<java:validate-type
    class="com.me.Person"
    instance="#[vars.jane]"
    acceptSubtypes="true" />
```

> [!NOTE] Omitting the `Accept subtypes` parameter defaults its value to `true`.

The previous code is equivalent to:

```xml
<java:validate-type
    class="com.me.Person"
    instance="#[vars.jane]" />
```

However, when setting the `Accept subtypes` to `false`:

```xml
<java:validate-type
    class="com.me.Person"
    instance="#[vars.jane]"
    acceptSubtypes="false" />
```

During execution the operation throws an error, interrupting the flow and passing control to the
[error handler](https://docs.mulesoft.com/mule-runtime/latest/mule-error-concept).

## Validate Type Function in DataWeave

In this example, the Java module provides a DataWeave `isInstanceOf()` function that returns `true`
or `false` if an object is an instance of a class. Unlike the `Validate type` operation, it does not
throw an error when it evaluates to `false`.

```xml
<choice>
    <when expression="#[Java::isInstanceOf(vars.jane, com.me.Employee)]">
        <flow-ref name="routeToCompany" />
    </when>
</choice>
```

## See Also

- [Invoke Methods](https://docs.mulesoft.com/java-module/latest/java-invoke-method)

- [Work With Throwables](https://docs.mulesoft.com/java-module/latest/java-throwable)
