title: Infinite recursion in Getter in Kotlin
body: <p>I am familiar with Java, but I am having difficulty working with Kotlin. </p>
  
  <p>To illustrate my question, here is some Java Code. If the getter finds the field to be NULL, it initializes the field, before returning the field. </p>
  
  <pre><code>package test;
  
  public class InitFieldJava {
  
      private final static String SECRET = "secret";
      private String mySecret;
  
      public String getMySecret() {
          if(mySecret == null) initMySecret();
          return mySecret;
      }
  
      private void initMySecret() {
          System.out.println("Initializing Secret ....");
          mySecret = SECRET;
      }
  
      public static void main(String[] args) {
          InitFieldJava field = new InitFieldJava();
          System.out.println(field.getMySecret());
      }
  }
  </code></pre>
  
  <p>Can I do something like the above in Kotlin. My attempt in Kotlin looks like this:</p>
  
  <pre><code>package test
  
  class InitFieldKotlin {
      private val SECRET = "secret"
      private var mySecret: String? = null
      get() {
          if (mySecret == null) initMySecret() //Infinite Recursion!!!
          return mySecret
      }
  
      private fun initMySecret() {
          println("Initializing Secret ....")
          mySecret = SECRET
      }
  
      companion object {
          @JvmStatic
          fun main(args: Array&lt;String&gt;) {
              val field = InitFieldKotlin()
              println(field.mySecret)
          }
      }
  }
  </code></pre>
  
  <p>My problem is that this results in infinite recursion: </p>
  
  <pre><code>Exception in thread "main" java.lang.StackOverflowError
      at test.InitFieldKotlin.getMySecret(InitFieldKotlin.kt:7)
      at test.InitFieldKotlin.getMySecret(InitFieldKotlin.kt:7)
      at test.InitFieldKotlin.getMySecret(InitFieldKotlin.kt:7)
      at test.InitFieldKotlin.getMySecret(InitFieldKotlin.kt:7)
  </code></pre>
  
  <p>I’d appreciate knowing what I’m doing wrong.  </p>