Race condition in Java occurs only in a multithreaded environment .
It occurs when more than one thread try to access a shared resource at the same time
Access can be defined as read,modify and write it back to the main memory.

Now just think why would a race condition occur.
Just take a example below.
If one thread reads a value of a variable as 1.
1 signifies the number of times the value of the thread has beeen read .
At the same time another thread reads the value as 1.
Then the first thread increments the value of the variable by 1 and writes to memory.
Then the second thread increments the value of the variable by 1 and writes to memory.
Here the final value of the variable would be 2 and not 3 which is wrong.
So to solve this race condition, we can use synchronization.
Local variables are thread safe while class variables are not if the class object is shared among multiple threads.
Thanks for reading!!!