+ 2

Help me in java code

https://code.sololearn.com/c8TOe95kSzi2/?ref=app Help me to understand the last line of this program System.out.println(Animal.numAnimal); And also it print output 3 how?

3rd Feb 2019, 9:56 AM
Saad Saleem
Saad Saleem - avatar
7 Answers
+ 7
You have a static variable numAnimal in the Animal class and the following constructor: Animal(){ numAnimal++; } Each time an object of type Animal is instantiated, numAnimal is incremented by 1. Based on the code, 3 objects of type Animal are created (cat and dog both of which are child classes of Animal and an Animal object). Thus, you get 3 when you print out the numAnimal variable. Check this out to understand the static variable: https://www.sololearn.com/learn/Java/2159/?ref=app Based on your profile, you haven't finished the Java tutorial yet. I suggest you review some of its topics if you are still having issues. The comments section in the lesson are a big help. Good luck and happy coding!
3rd Feb 2019, 10:10 AM
Lambda_Driver
Lambda_Driver - avatar
+ 6
Lines 41, 43 and 46. Note the use of the keyword "new" which is the syntax used to create an object. https://www.sololearn.com/learn/Java/2155/?ref=app
3rd Feb 2019, 10:25 AM
Lambda_Driver
Lambda_Driver - avatar
+ 6
No problem. When I started out, I didn't know what static means or does even if I use it all the time. We learn from our mistakes. In time once you gain more knowledge, it will be your turn to answer other people's questions in the q & a Saad Mughal. I hope you will be able to help others when the time comes.
3rd Feb 2019, 10:37 AM
Lambda_Driver
Lambda_Driver - avatar
+ 2
I don't understand where I Create three objects of type Animal please mention the line numbers
3rd Feb 2019, 10:21 AM
Saad Saleem
Saad Saleem - avatar
+ 2
Lambda_Driver I understand it Now very thankful to you
3rd Feb 2019, 10:31 AM
Saad Saleem
Saad Saleem - avatar
+ 2
The problem with me is that I not focoused on static
3rd Feb 2019, 10:32 AM
Saad Saleem
Saad Saleem - avatar
+ 1
Lambda_Driver I also wanated that I will help others like you and I also practice and learned hard to do that.
3rd Feb 2019, 10:41 AM
Saad Saleem
Saad Saleem - avatar