I am busy solving the task cats in the Universe. But what I do not understand is that objects were made. What I have learned so far is that there are no object needed for a static variable. So I am wondering why they are made?
Why are objects made in this task?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Hashirama
28 December 2018, 18:08
Yes we don't need any object to reference a static method from a static context. But that doesn't mean we can't make one if we like. However, in this very code, we want to increase the value of the count variable to 2 simple by creating 2 objects of the Cat class and using those objects call and increment the count variable's value.
Even without the objects, we could do this:
Cat.count++;
Cat.count++;
And still have our output as 2.
So the reason why they made those objects is prolly for simplicity/explanation sake. Not necessarily because we need them.
Hope this helps.
+5
Michael Martin
19 December 2018, 14:44
It looks like that Cat class is only static so it can be accessed by the static method main(). If it was in its own file, then it wouldn't be static, but they are just making it easier for you.
+1