title: Why does the nested class in a ConfigurationProperties class need to be static?
body: <p>I am using an <code>application.yml</code> file as the external configuration in my spring boot application which is bound to <code>AppConfig.java</code> using the <code>@ConfigurationProperties</code> annotation. Inside <code>AppConfig.java</code> I have nested classes based on the hierarchy in <code>application.yml</code>. When I use <code>static</code> to declare the nested classes, everything works fine. But recently in a project I missed <code>static</code> for one of the nested classes and it led to a <code>NullPointerException</code>. Through online resources, I have read when to and when not make nested classes static. However, I need to understand how the binding of <code>application.yml</code> and <code>AppConfig.java</code> occurs in spring boot and why the nested classes need to be <code>static</code>.</p>
  <h3>application.yml</h3>
  <pre><code>spring:
    foo:
      host: localhost
    bar:
      endpoint: localhost
  </code></pre>
  <h3>AppConfig.java</h3>
  <pre><code>@Component
  @ConfigurationProperties(&quot;spring&quot;)
  public class AppConfig {
      private Foo foo;
      private Bar bar;
      public static class Foo {
          private String host;
          // getter, setter
      }
      public class Bar {
          private String host;
          // getter, setter
      }
      //getters, setters
  }
  </code></pre>
  <p>When I Autowire <code>AppConfig.java</code> in other classes, <code>appConfig.getFoo()</code> works fine but <code>appConfig.getBar()</code> leads to a <code>NullPointerException</code>.</p>