Data members in DLL

Hello,
I am creating a DLL for JNI and therefore have a question. I create a class in Java with the native method. Then I create many objects of this class. The native method implementation contains beside the implementation of a method a few member variables. E.g.


jint a;

JNIEXPORT void JNICALL
Java_Refs_displayMsg (JNIEnv * env, jobject obj, jint b)
{
a = b;
}


My question is how this member data variable is being stored? Is it being stored per class, per object or in some other way? So, for example, if I call this native method in relation to one object - “obj1.displayMsg(1)” and then in relation to another object like - “obj2.displayMsg(3)”, would the variable “a” be changed? And how and where the system would store this value?
Also where I may find more information on that subject?

Thank you,
Kostya.

This is just ordinary C storage, and works in exactly the same way - in layman’s terms, there’s one ‘a’, and it’s in your DLL, and not associated with any java classloader.

Cas :slight_smile:

In other words - it is probably broken :slight_smile:

If you have multiple objects you have only one instance of ‘a’. ‘a’ is like a typical static variable in a Java class the way you have written it.

Public static, at that.

Cas :slight_smile:

OK, thank you,
But who is managing its’ state? Is it the OS that does that or JVM? Is there any way to “instantiate” this method? Or then I would have to create objects in C++?

The OS manages loading and unloading DLLs. Think of a DLL as a single static class, and the OS a single big classloader in which it lives.

Cas :slight_smile:

OK,
But still how does the data “live” in a static class then? I tried it out with launching the same application at the same time but with different parameters, it looks like the data in this DLL “lives” per application (which fits well with the concept of a shared library).


import java.awt.;
import java.awt.event.
;
import javax.swing.*;

class Refs extends JFrame {
public native void displayMsg(int i);

public Refs()
{	super("test5");
	addWindowListener
	(	new WindowAdapter()
		{	public void windowClosing(WindowEvent e)
			{

				System.exit(0);

			}
		}
	);

	


	setSize(325,110);
	setResizable(false);
	setVisible(true);
}

static {
    System.loadLibrary("contents");
}

public static void main(String[] args) {
    new Refs().displayMsg(Integer.parseInt(args[0]));
}

}


GUI is to keep an application alive.
Thank you for your help.

The global (static) variables of a dll is per-process, that is, there is one variable for each process that has loaded the library. So actually it is not even equivalent to a static variable in java, since in java, static variables are per-classloader.

  • elias

In which case, consider each process to have its own classloader and you can amend my previous post to get an even more accurate one :slight_smile:

Cas :slight_smile:

OK,
It’s much more clear now, thank you!
And what about threads? Will they be considered by OS as separate processes?
As far as I understood from JNI tutorial there is one JNI interface per thread.

No, threads are threads. Processes are processes! All threads of a process share the heap of that process, and there’s no synchronization in JNI code so beware of that too!

Cas :slight_smile: