- Qt 6.11
- Qt Core
- C++ Classes
- JObject
- Qt 6.11.2 Reference Documentation

# QtJniTypes::JObject Class

template <typename Type> class QtJniTypes::JObject

The JObject template in the QtJniTypes namespace is the base of declared Java types. More...

Header: | `#include <JObject>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS Core)` `target_link_libraries(mytarget PRIVATE Qt6::Core)`
qmake: | `QT += core`
Since: | Qt 6.8

- List of all members, including inherited members

## Public Functions

| JObject()
| JObject(QJniObject &&other)
| JObject(const QJniObject &other)
| JObject(jobject other)
auto | callMethod(const char *method, Args &&... args) const
QByteArray | className() const
auto | getField(const char *field) const
bool | isValid() const
jclass | objectClass() const
auto | setField(const char *field, T &&value)
QString | toString() const

## Static Public Members

auto | callStaticMethod(const char *methodName, Args &&... args)
QtJniTypes::JObject<Type> | construct(Args &&... args)
QtJniTypes::JObject<Type> | fromJObject(jobject object)
QtJniTypes::JObject<Type> | fromLocalRef(jobject ref)
auto | getStaticField(const char *field)
bool | isClassAvailable()
bool | registerNativeMethods(std::initializer_list<JNINativeMethod> methods)
auto | setStaticField(const char *field, T &&value)

## Detailed Description

This template gets specialized when using the Q_DECLARE_JNI_CLASS macro. The specialization produces a unique type in the QtJniTypes namespace. This allows the type system to deduce the correct signature in JNI calls when an instance of the specialized type is passed as a parameter.

Instances can be implicitly converted to and from QJniObject and jobject, and provide the same template API as QJniObject to call methods and access properties. Since instances of JObject know about the Java type they hold, APIs to access static methods or fields do not require the class name as an explicit parameter.

See also Q_DECLARE_JNI_CLASS.

## Member Function Documentation

### JObject::JObject()

Default-constructs the JObject instance. This also default-constructs an instance of the represented Java type.

### `[noexcept]` JObject::JObject(QJniObject &&other)

Move-constructs a JObject instance from other.

### JObject::JObject(const QJniObject &other)

Constructs a JObject instance that holds a reference to the same jobject as other.

### JObject::JObject(jobject other)

Constructs a JObject instance that holds a reference to other.

### template <typename Ret = void, typename... Args> auto JObject::callMethod(const char *method, Args &&... args) const

Calls the instance method method with arguments args, and returns the result of type `Ret` (unless `Ret` is `void`). If `Ret` is a jobject type, then the returned value will be a QJniObject.

See also QJniObject::callMethod().

### `[static]` template <typename Ret = void, typename... Args> auto JObject::callStaticMethod(const char *methodName, Args &&... args)

Calls the static method methodName with arguments args, and returns the result of type `Ret` (unless `Ret` is `void`). If `Ret` is a jobject type, then the returned value will be a QJniObject.

See also QJniObject::callStaticMethod().

### QByteArray JObject::className() const

Returns the name of the Java class that this object is an instance of.

See also objectClass() and QJniObject::className().

### `[static]` template <typename... Args> QtJniTypes::JObject<Type> JObject::construct(Args &&... args)

Constructs a Java object from args and returns a JObject instance that holds a reference to that Java object.

### `[static]` QtJniTypes::JObject<Type> JObject::fromJObject(jobject object)

Constructs a JObject instance from object and returns that instance.

### `[static]` QtJniTypes::JObject<Type> JObject::fromLocalRef(jobject ref)

Constructs a JObject that holds a local reference to ref, and returns that object.

### template <typename T> auto JObject::getField(const char *field) const

Returns the value of the instance field field.

See also QJniObject::getField().

### `[static]` template <typename T> auto JObject::getStaticField(const char *field)

Returns the value of the static field field.

See also QJniObject::getStaticField().

### `[static]` bool JObject::isClassAvailable()

Returns whether the class that this JObject specialization represents is available.

See also QJniObject::isClassAvailable().

### bool JObject::isValid() const

Returns whether the JObject instance holds a valid reference to a jobject.

See also QJniObject::isValid().

### jclass JObject::objectClass() const

Returns the Java class that this JObject is an instance of as a jclass.

See also className() and QJniObject::objectClass().

### `[static]` bool JObject::registerNativeMethods(std::initializer_list<JNINativeMethod> methods)

Registers the Java methods in methods with the Java class represented by the JObject specialization, and returns whether the registration was successful.

See also QJniEnvironment::registerNativeMethods().

### template <typename Ret = void, typename T> auto JObject::setField(const char *field, T &&value)

Sets the value of the instance field field to value.

See also QJniObject::setField().

### `[static]` template <typename Ret = void, typename T> auto JObject::setStaticField(const char *field, T &&value)

Sets the static field field to value.

See also QJniObject::setStaticField().

### QString JObject::toString() const

Returns a QString with a string representation of the Java object.

See also QJniObject::toString().