I not sure what this method is called or how or why this works??
Any thoughts?
public class Cat {
private static int catCount = 0;
public Cat() {
catCount++;
}
public static int getCatCount() {
//write your code here
return catCount;
}
public static void setCatCount(int catCount) {
//write your code here
Cat.catCount = catCount;
}
public static void main(String[] args) {
}
}
Cat.catCount = catCount;
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Salim
11 November 2019, 06:14
shukran!!!
0
Nithin
15 July 2019, 02:20
The catCount variable defines the number of cat objects created. whenever you create a new cat object catCount will be increased by one(see the code in constructor). if you want to set a specific value to it you will call setCatCount(int catCount) method. it will assign the passed value to the catCount variable.
but here the parameter(in setCatCount method) and the variable for which you want to set this value(line 2) both have same name "catCount". so you need to differentiate which is which
if it is a non-static variable you will do this using :
if it is a static variable you will use :
+8