title: Jenkins shared library
body: <p>I am in the process of putting together a shared library for my Jenkins builds. I haven't really used groovy much before so at the moment I am following the docs (always a good start :-)) and have created a file located at <code>/vars/myFile.groovy</code> for example.</p>
  
  <p>Now I have a list (shortened in the example below) that I need to use and have placed it within a method for now, but am I right in thinking it would be better outside of the method so that it is only instantiated once rather than every time the method is called?</p>
  
  <p><code>/vars/myFile.groovy</code></p>
  
  <pre><code>#!/usr/bin/groovy
  
  def slack_handle(String dev_name) {
    developerList = [
       [name: "Richard Lewis", slack_handle: "&lt;@richardlewis123&gt;"],
       [name: "Mark Turner", slack_handle: "&lt;@markTurner123&gt;"]
      ]
    return developerList.find {it['name'] == dev_name}?.get("slack_handle")
  }
  
  def other_method() {
  
  }
  
  def another_method() {
  
  }
  </code></pre>
  
  <p>Then in my Jenkinsfile i can just do</p>
  
  <pre><code>SLACK_HANDLE = slackNotification.slack_handle("Richard Lewis")
  echo "${SLACK_HANDLE}"
  "&lt;@richardlewis123&gt;"
  </code></pre>
  
  <p>How can i declare the list outside of the method to then use within my <code>slack_handle</code> method in this shared library</p>
  
  <p>I have tried</p>
  
  <pre><code>final def developerList = [
       [name: "Richard Lewis", slack_handle: "&lt;@richardlewis123&gt;"],
       [name: "Mark Turner", slack_handle: "&lt;@markTurner123&gt;"]
      ]
  
  def slack_handle(String dev_name) {
    return developerList.find {it['name'] == dev_name}?.get("slack_handle")
  }
  
  def other_method() {
  
  }
  
  def another_method() {
  
  }
  </code></pre>
  
  <p>But when the Jenkins job runs <code>developerList</code> is undeclared.</p>
  
  <p>So my question is, should the List be declared outside of the method or in this instance is it OK where it is?</p>