Passing parameter back and forth
/**
@version 1.10 1999-11-13
@author Cay Horstmann
*/
#include "Employee.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_Employee_raiseSalary(JNIEnv* env, jobject this_obj, jdouble byPercent)
{
/* get the class */
jclass class_Employee = (*env)->GetObjectClass(env, this_obj);
/* get the field ID */
jfieldID id_salary = (*env)->GetFieldID(env, class_Employee, "salary", "D");
/* get the field value */
jdouble salary = (*env)->GetDoubleField(env, this_obj, id_salary);
salary *= 1 + byPercent / 100;
/* set the field value */
(*env)->SetDoubleField(env, this_obj, id_salary, salary);
}
////
/*
This program is a part of the companion code for Core Java 8th ed.
(http://horstmann.com/corejava)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @version 1.10 1999-11-13
* @author Cay Horstmann
*/
public class Employee
{
public Employee(String n, double s)
{
name = n;
salary = s;
}
public native void raiseSalary(double byPercent);
public void print()
{
System.out.println(name + " " + salary);
}
private String name;
private double salary;
static
{
System.loadLibrary("Employee");
}
}
////
/*
This program is a part of the companion code for Core Java 8th ed.
(http://horstmann.com/corejava)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @version 1.10 1999-11-13
* @author Cay Horstmann
*/
public class EmployeeTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("Harry Hacker", 35000);
staff[1] = new Employee("Carl Cracker", 75000);
staff[2] = new Employee("Tony Tester", 38000);
for (Employee e : staff)
e.raiseSalary(5);
for (Employee e : staff)
e.print();
}
}
JNIParameterTest.zip( 2 k)Related examples in the same category