This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

from bunch of toughest questions by paul(question11)  RSS feed

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this code is little modified from original one.
ouput:10 10 2 ok fine.
but if at line1 we remove static,then it shows size=0 why?
i am lacking somewhere?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since at line1 Map is declared static its no more different for different instances.
So all the instances of BookStore will share this and any changes made by anyone of them will be reflected by the others.

Now, when you remove 'static' at line 1, you are making Map an instance variable and so each instance of BookStore will have its own copy of map.
At line 2, you are making a new instance of BookStore which has its own copy of Map and this Map doesn't have anything so it returns the size '0'.

HTH
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hum..thanks .
that is why we prefer to invoke static things by a class-name instead of new instance(i.e.later is a bad practice).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!