---
schema_version: "1"
id: b11693f1debda8ea7588b1a1aa916962f53583ef75f5fe6f78fc12d91777a445
title: Reusing Types from Java Classes
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.11/dataweave-type-reuse-java-classes.md
canonical_url: https://docs.mulesoft.com/dataweave/2.11/dataweave-type-reuse-java-classes
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 8d1aec019c7ed05de864798ea9fabf7f6aeba1581eb81f57a1729ad291af5e3e
description: MuleSoft Documentation Site
release: "2.11"
latest: false
---
---
product: DataWeave
version: 2.11
---

# Reusing Types from Java Classes

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

Use the import directive with the `java!` prefix when loading a Java class. The prefix `java!` tells
DataWeave to use the Java module loader. After the module loads, you can call constructors,
variables, and functions (see [Call Java Methods with DataWeave](https://docs.mulesoft.com/dataweave/2.11/dataweave-cookbook-java-methods)).
The class type is also available. DataWeave exposes the type of the Java class with the type name
`Class`.

Java classes are mapped to the DataWeave types listed in
[Java Value Mapping](https://docs.mulesoft.com/dataweave/2.11/dataweave-formats-java#java-value-mapping). If a Java class is not specified in
the table, DataWeave treats the class as `JavaBean` and maps it to a DataWeave `Object` type. The
keys in that object match the names of the properties in the Java class and their values correspond
to the DataWeave type that matches the property class. The following example shows how DataWeave
takes all properties from the Java getters:

Product Class:

package org.mycompany;

public class Product { private String name; private int price;

    public Product(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public int getPrice() {return price;}
    public void setPrice(int price) {this.price = price;}

}

Loading the type for this Java class has the same effect as declaring this DataWeave type:

type Class = { name?: String | Null, price?: Number | Null }

The following example uses the type `Product::Class` as any other type in DataWeave. Creating a
DataWeave object by calling the function `Product::new` (which calls the constructor of the Java
class) also matches the type `Product::Class`. Notice that the import directive lacks `from` and
instead uses the type `Class` prefix `Product::`. The prefix enables more declarative use of the
type and avoids collisions with any other imported `Class` types:

DataWeave Script:

```dataweave
%dw 2.0
output json
import java!org::mycompany::Product

var aBook: Product::Class = {name: "Learn DW", price: 123}
fun description(p: Product::Class) = "The product: $(p.name), costs: $(p.price)"

type Order = {
    product: Product::Class,
    date: LocalDateTime
}
---
{
    a: Product::new("DW lang", 321) is Product::Class,
    b: description(aBook),
    c: {product: aBook, date: |2022-12-18T14:00:00|} is Order
}
```

Output:

{ a: true, b: "The product: Learn DW, costs: 123", c: true }
