+ 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?
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!
+ 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
+ 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.
+ 2
I don't understand where I Create three objects of type Animal please mention the line numbers
+ 2
Lambda_Driver I understand it Now very thankful to you
+ 2
The problem with me is that I not focoused on static
+ 1
Lambda_Driver I also wanated that I will help others like you and I also practice and learned hard to do that.