I have my main class from which i call my sub class.
My sub class contains some public static variables like
public class SubClass2 extends Main {
public static long a = 0;
public static long b = 0;
public static long c= 0;
public void Analyze(int number)
{
b=2;
//some code
}
}
Where as in main i call the object of the SubClass2.I want everytime when i make the new object of the subclass2 in main then it initializes
all the variables =0 but when i take the print statement of the variable b.It prints out like 4.It adds up the previous value with the new value.

Your fields should not be declared as
staticin that case. This is why they’re not being initialised each time. Astaticfield is initialised once only, then shared by every instance of the class, and depending on accessibility, also outside of the class.The logic that led to the value
4must be in the code you’ve replaced with//some code, but this ins’t really relevant here.If for whatever reason these really should be
staticfields that are initialised each time an instance is instantiated, then you would have to initialise them manually in the class’s constructor. But I’d seriously question the design that leads to this situation…