Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Define this map as static - it will be in memory until class loader who loaded this class is not garbage collected. </p> <p>I say above by refering : <a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/execution.html#12.7" rel="nofollow">http://docs.oracle.com/javase/specs/jls/se5.0/html/execution.html#12.7</a></p> <p>Static member are linked to class and above specification says that classes will not be unloaded until class loader is in place. </p> <p>Whereas objects do get garbage collected. Hence suggested to make map static(make it public too in case needs access from outside).</p> <p>And for loading file into map</p> <p>store it in file as </p> <p>key1=value1 key2=value2 .... ....</p> <p>now use BufferedReader as below</p> <pre><code>BufferedReader reader = new BufferedReader(new FileReader(new File("pathname"))); String line = null; Map&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;();// it should be static - whereever you define while ((line = reader.readLine()) != null) { if (line.contains("=")) { String[] strings = line.split("="); map.put(strings[0], Integer.parseInt(strings[1])); } } </code></pre> <p>Class loader is something which loads classes in memory while starting the application. Tomcat also has its classloader which loads required classes in memory(Classes and not objects). Now we know that static variables are associated with class and not object. So static members are loaded in memory along with class. IN many other cases you would be creating object of the class and use it. If you have millions of objects loaded in memory- You will be soon short of it. So java have something called garbage collector. This garbage collector removes unwanted/old objects from memory to recycle it. Garbage collector removes objects not classes and hence static member still remains in memory.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload