title: Static List of Objects out Class in Kotlin
body: <p><a href="https://stackoverflow.com/questions/56592278/static-list-of-all-objects-of-class">Static List of all Objects of Class</a><br>
  Is this working with Kotlin if yes can you provide an example?</p>
  
  <p>Right now my interface does not have the List and i am creating a companion Object in both implementations. But it feels wrong somehow.... Pls help me to figure this out :)</p>
  
  <p>I have four ways right now to do this i will show two here because the fourth one would require a more complex <strong>UML</strong>. Mybe this is actually how you do it if not maybe one of you has an idea ;)</p>
  
  <p>My Code in Kotlin approach one: </p>
  
  <pre><code>interface IMyClass {
  }
  
  class MyClassOne : IMyClass {
       companion object {
          var myClassList: ArrayList&lt;IMyClass&gt; = ArrayList()
      }
      constructor() {
          println(myClassList)
      }
  }
  
  class MyClassTwo : IMyClass {
       companion object {
          var myClassList: ArrayList&lt;IMyClass&gt; = ArrayList()
      }
      constructor() {
          println(myClassList)
      }
  }
  </code></pre>
  
  <p>My Code in Kotlin approach two: </p>
  
  <pre><code>interface IMyClass {
      companion object {
         var myClassList: ArrayList&lt;IMyClass&gt; = ArrayList()
      }
  }
  
  class MyClassOne : IMyClass {
      constructor() {
          println(IMyClass.myClassList)
      }
  }
  
  class MyClassTwo : IMyClass {
      constructor() {
          println(IMyClass.myClassList)
      }
  }
  </code></pre>
  
  <p>My Code in Kotlin approach three: </p>
  
  <pre><code>interface IMyClass {
      var myClassList: ArrayList&lt;IMyClass&gt;
  }
  
  class MyClassOne {
      companion object : IMyClass{
          override var myList: ArrayList&lt;IMyClass&gt;
              get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
              set(value) {}
      }
  }
  
  class MyClassTwo {
  companion object : IMyClass{
          override var myList: ArrayList&lt;IMyClass&gt;
              get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
              set(value) {}
      }
  }
  </code></pre>
  
  <p>My Code in Java:</p>
  
  <pre><code>public interface IMyClass {
   List&lt;IMyClass&gt; myClassList = new ArrayList&lt;&gt;();
  }
  
  public class MyClassOne implements IMyClass {
      public MyClassOne() {
          System.out.println(myClassList );
      }
  }
  
  public class MyClassTwo implements IMyClass {
      public MyClassTwo() {
          System.out.println(myClassList);
      }
  }
  </code></pre>