im not able to solve the code. please do help me
someone please post the code
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Voland12 Voland
19 March 2019, 19:48
catCount is static variable. Try return only catCount without assignment to class.
+1
Jyothi
2 January 2019, 14:13
package com.codegym.task.task06.task0608;
/*
Static methods for cats
*/
public class Cat {
private static int catCount = 0;
public Cat() {
catCount++;
}
public static int getCatCount() {
//write your code here
return Cat.catCount;
}
public static void setCatCount(int catCount) {
//write your code here
Cat.catCount = catCount;
}
public static void main(String[] args) {
}
}
+13
Jason
21 June 2019, 05:33
so in the set method why is it "Cat." instead of "this." this time around. I guess i am still a bit confused with that at this point
+2
Onyejeme Emmanuel
3 December 2019, 15:47
why we do not use this is because the catCount is a static variable i.e it is created only once irrespective of the number of objects created.
To access static variable, we use the format className.variable
e.g Cat.catCount; // Cat is the class name, catCount is the static variable.
if the variable was not static, then we use this keyword to refer to it
e.g public int catCount;
any object created has a reference to this variable. if i create 10 objects, it means i have 10 catCount variables
but if it was static, even if i have 10 objects, i have only ONE catCount variable.
+13
Joe M
24 October 2020, 18:32
super helpful sir, thank you
+1