In C# I can declare a static var in a class. E.g : to count the create instances of class “foo”. Like so
class foo
{
static int countFoo =0;
public foo()
{
countFoo++;
}
}
Now – for each foo created I can use a getFooCount method to know how many instances were created.
How do you do that in Java?
I tried to do the same and it doesn’t work.
Please explain why and how. Thanks!
No, you can do exactly the same and it will work. Sample code:
(It would have helped if you’d shown what you’d tried…)