Why i don't have access to variable catCount in the main method? I've tried creating the getter method for it but it didn't work.
package com.codegym.task.task06.task0613;
/*
Cat and statics
*/
public class Solution {
public static void main(String[] args) {
for (int i = 0; i < 10; i++){
Cat cat = new Cat();
}
System.out.println(catCount);
}
public static class Cat {
public static int catCount = 0;
public Cat() {
catCount++;
}
}
}
What is the problem here?
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Roman Ivanov
2 January 2019, 13:06solution
This is because this variable is declared in the Cat class, but the main method from which this variable is accessed is in the Solution class. You should use Cat.catCount instead of just catCount.
+4
Zbigniew
2 January 2019, 17:19
I wonder how could I not think of it. It was pretty obvious. :) Thank you very much!
0