I had this weird observation yesterday thought of sharing with you:
I was on the way from my home station (Journal Square) to office (33d Street NY) in train and as always I had taken the corner most seat of the wagon and was busy reading some newspaper article . After couple of stops I realized a super hot blonde girl with the minimal of formal outfit you could think of was sitting opposite to me . As most of us do , I too tried avoiding a stare towards her but again as always couldn’t avoid and somehow or the other kept on looking at her ( for some reason , there was this James blunt’s song , You’r beautiful started playing in my head ) . Anyways 23rd street arrived and again as always she left the train without even noticing me and the respect I had grown for her beauty J . So with heavy heart my stop arrived and I got down thinking of her and felt I LOVE BLONDE GIRLS.
Then as every day morning I proceeded towards a star bucks coffee shop next to my office to pick my latte ( as I have realized that if you want to be noticed in NY , you need to carry star bucks coffee even if you end up dumping it in your restroom, but somehow I like their latte). And as I entered I saw Christine again , a dark eyed ,dark haired beautiful Mexican girl, smiling and asked me do I need latte again ? ( as she suggests me many times to try some other coffee’s too at that place , but I don’t end up taking risk) . I picked my coffee , paid my bill and came out of the shop thinking I LOVE MEXICAN GIRLS.
At this moment , I realized what happened to my love for blonde girls ? Why did it change suddenly?
The first thing that came to my mind was , BLONDE and MEXICAN are 2 classes in my brains which are loaded and with every girl of that type and instance is created for them and if they are beautiful they are persisted else made available for garbage collection. So when I saw the girl in train , my JVM loaded a class called Blonde and quickly made an instance and filled all the variables in it J . Then when I went to coffee shop , it loaded a class called MEXICAN , again made a instance of it with features of
Christine . But what happened to my class and object of blonde , it should have been still live. How does brain takes care of so many different classes of girls ?
The only reason I thought was if classes were getting unloaded back to main storage from my RAM . So does it even happen in JAVA . But I have never faced it in JAVA. neither I do take care of it while coding .
So I searched JVM doc to find the answer and I found it on a short note up which says ‘A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector’ .
So if we write a custom class loader without much care and this gets unloaded so all the classes it loaded will get unloaded.
The dangers could be : If any class which was loaded before get unloaded ( due to classloader) but gets loaded again , all the static blocks would run again . And if you had some DB calls or Business logic present , this might be even more dangerous .
I understand it’s not a daily activity to write a classloader but thought of sharing with you if you some day end up finding a issue like this.
More information on http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.7