Imagine two simple java applications.
Both of them are implementing the same JAR file containting an enum file like this:
enum enum1{
value1;
}
In both the applications I print the enum1.value1´s hashcode.
System.out.print(enum1.value1.hashCode());
How does the JVM work since the hashcodes are not equal even if the JAR file that the two applications implement is the same?
Why are not the hashcodes equal since it is the same JAR file the both applications implement?
EDIT
I have two applications that access a database. They are run within the same JVM. I would like to implement a locking mechanism so that when application 1 is writing to the database, application 2 has to wait for its turn (that is when the lock has been released by application 1). My solution is, if possible, to make an interface that has some ReentrantLocks declared or an enum that should act as lock and is used by the two applications. But then the instance of the interface/enum should be equal in both the applications, because you can only synchronize the same object.
EDIT 2
This is the architecture:
App1.jar Commons.jar App2.jar
App1Main.class Commons.class App2Main.class
Both App1 and App2 includes Commons.jar. The commons.class is just a simple singleton class.
In both App1 and App2 I print the commons instance hashcode:
System.out.println(Commons.getInstance().hashCode());
Both java applications are run like “java -jar app1” and “java -jar app2” so there are two processes when they are running.
But they print different hashcodes and that is, what I belive (correct me if I am wrong), because they have been loaded by different class loaders. But the mystic arrives when I print the classloader in both apps:
System.out.print(ClassLoader.getSystemClassLoader().hashCode());
Then the hashcode is equal in both applications.
It’s not clear whether you’re talking about two applications within the same process or not. Even if you are, if those two applications have separate
ClassLoaderinstances loading the same jar file, the two enum types are different types as far as the JVM is concerned. If you want a single type within a process used by two applications, it has to be loaded by a single classloader.