wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes from bunch of toughest questions by paul(question11) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "from bunch of toughest questions by paul(question11)" Watch "from bunch of toughest questions by paul(question11)" New topic
Author

from bunch of toughest questions by paul(question11)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


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?


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
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


SCJP 1.6 96%
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

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).
 
Don't get me started about those stupid light bulbs.
 
subject: from bunch of toughest questions by paul(question11)
 
Similar Threads
Enthuware Toughest questions on Collections
hashCode(), equals(): Got question right, but for the wrong reasons
My Array
BinarySearch
Arraylist