class ref

Use (conceptually) non-owning type ref when type of reference (local, global or weak global) doesn’t matter. Use local_ref, global_ref or weak_ref explicitly to specify owning semantics. For example, use global_ref or weak_ref to store a reference in your C++ object when returning control to Java.

#include <scapix/java_api/java/lang/String.h>

namespace jni = scapix::jni;
using namespace scapix::java_api;

void test()
{
    // canonical form:
    auto s = jni::new_object<java::lang::String>();

    // same as:
    jni::ref<java::lang::String> s = jni::new_object<java::lang::String>();

    // effectively same as:
    jni::local_ref<java::lang::String> s = jni::new_object<java::lang::String>();

    // create global reference from returned local reference:
    jni::global_ref<java::lang::String> s = jni::new_object<java::lang::String>();

    // create weak global reference from returned local reference:
    jni::weak_ref<java::lang::String> s = jni::new_object<java::lang::String>();
}

While ref is conceptually a non-owning type, it is actually implemented to take ownership when moved to, and not to take ownership when copied to. This allows both zero overhead parameters (copy) and safe return of temporaries (move).