How to invoke assignment operator in a native method?

Discussion in 'Java' started by HappyHippy, Aug 30, 2006.

  1. HappyHippy

    HappyHippy Guest


    Could you please help me to figure out how to invoke assignment operator
    in a native method (C/C++ side)?
    For StringBuffer "append" method I am doing the following:
    jmethodID methodId = env->GetMethodID(objClass, "append",

    Can I do something like this for Integer's assignment operator?
    Something like:

    jmethodID methodId = env->GetMethodID(objClass, "=",

    And then just invoke it via: env->CallObjectMethod(..., methodId,....)

    Thank you!
    HappyHippy, Aug 30, 2006
    1. Advertisements

  2. Please explain exactly what it is you're trying to achieve, not how
    you hope to achieve it.

    To see what methods the Integer class has, use javap -s. The only way
    to assign a value to an Integer is to specify the value when you
    invoke the constructor.

    If your class has an Integer field and you want to assign an
    (existing) Integer *object* to it, use SetObjectField().

    Please don't multipost. I've *crossposted* this reply and set
    followups to c.l.j.programmer.

    Gordon Beaton, Aug 30, 2006
    1. Advertisements

  3. HappyHippy

    HappyHippy Guest

    I want to achieve the effect of the following assignment (which can be
    done in Java)

    SomeNumber = 5;

    where SomeNumber is of type java.lang.Integer

    But in a native method I do not have an Integer object, I have jobject
    So, my question is: is it possible to invoke assignment operator "=" on
    the object referenced by jobject?
    As far as I know (I don't know much about Java...) assignment operator
    is kind of special and it is "hidden" in Java.

    Thank you
    HappyHippy, Aug 30, 2006
  4. This is only possible due to autoboxing, and is shorthand for

    SomeNumber = new Integer(5);

    In native code, you do it by invoking the Integer constructor (an
    instance method with the name "<init>") then taking the resulting
    Integer and assigning it to the field in your object:

    /* create an Integer */
    jclass iclass = (*env)->FindClass(env,"java/lang/Integer");
    jmethodID mid = (*env)->GetMethodID(env,iclass,"<init>","(I)V");
    jobject i = (*env)->NewObject(env,iclass,mid,5);

    /* assign the field */
    jclass oclass = (*env)->GetObjectClass(env,myobj);
    jfieldID fid = (*env)->GetFieldID(env,oclass,

    Gordon Beaton, Aug 30, 2006
  5. HappyHippy

    Oliver Wong Guest

    This is basically what the compile does under the covers:

    Integer someNumber = Integer.valueOf(5);

    If you know how to express that in your native code, then you can
    perform the assignment.

    - Oliver
    Oliver Wong, Aug 30, 2006
  6. HappyHippy

    Chris Smith Guest

    In addition to Gordon and Oliver's replies, let me be explicit about
    something. When you write:

    SomeNumber = 5;

    which is equivalent to:

    SomeNumber = Integer.valueOf(5);

    you are not doing *anything* the object that SomeNumber currently points
    to. You are reassigning SomeNumber to point to a completely new object.
    You say you've got a jobject, but you don't say which one it is. The
    jobject that SomeNumber points *to* doesn't help you at all.
    Presumably, SomeNumber is a field (you can't access local variables of
    Java methods from JNI at all), and you need the jobject that *contains*
    the SomeNumber field. Once you've got that, then you can call
    SetObjectField as Gordon described.

    Incidentally, naming conventions are there for a reason, and are
    absolutely universal among professional Java programmers. Call the
    field someNumber, not SomeNumber.
    Chris Smith, Aug 31, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.