title: Return a value that was defined in map.forEach
body: <p>I have a Map named map passed as parameter to a function maptojson function i want to return the json for each k,v of the the map i have the following code .it shows the following error pointing to json &quot;Local variable inputJson defined in an enclosing scope must be final or effectively final&quot;</p>
  <pre><code>public static JSONObject maptojson(Map map){
  JSONObject Jsonobj = null;
    map.forEach(
      (k, v) -&gt; {
         Jsonobj = new JSONObject();//Local variable Jsonobj defined in an enclosing 
                                        scope must be final or effectively final
          Jsonobj.put(&quot;key&quot;, k);
           Jsonobj.put(val, v);
  
      });
    return Jsonobj;
   }
  </code></pre>