title: Is it ever ok to use public fields in Java?
body: <p>The question in the title is a little generic and many other similar questions have already been answered so I understand the general picture. Mainly, the use of a public field in a Java class creates dependance to the implementation details of the class, which leads to less mantainable software.</p>
  
  <p>With that in mind, I still got the feeling that I can use them somehow. To explain this, I will use an example: with the upcoming of Java 8's functional interfaces I finally can use a event model that I like, which is similar to how events are dealt with in C#.</p>
  
  <p>The event model is implemented in a package which is intended to be used as a third party API in all my projects.
  Those are the main classes:</p>
  
  <p>EventArgs.java</p>
  
  <pre><code>public class EventArgs implements Serializable {
  }
  </code></pre>
  
  <p>EventHandler.java</p>
  
  <pre><code>@FunctionalInterface
  public interface EventHandler&lt;T extends EventArgs&gt; {
      void fire(Object sender, T args);
  }
  </code></pre>
  
  <p>Event.java</p>
  
  <pre><code>public class Event&lt;T extends EventArgs&gt; {
  
      private final Set&lt;EventHandler&lt;T&gt;&gt; handlers = new LinkedHashSet&lt;&gt;();
  
      public void addEventHandler(EventHandler&lt;T&gt; handler) {
          handlers.add(handler);
      }
  
      public void removeEventHandler(EventHandler&lt;T&gt; handler) {
          handlers.remove(handler);
      }
  
      public void fire(Object sender, T args) {
          for (EventHandler&lt;T&gt; e : handlers) {
              e.fire(sender, args);
          }
      }
  }
  </code></pre>
  
  <p>What I usually like to do in my classes that want to fire events is something like this:</p>
  
  <pre><code>public class SomeClass {
  
      public final Event&lt;SomeEventArgs&gt; someEvent = new Event&lt;&gt;();
  
      public void someMethod() {
           // ...
           someEvent.fire();
      }
  }
  </code></pre>
  
  <p>and in the classes that want to subscribe the event:</p>
  
  <pre><code>public class SomeOtherClass {
      private SomeClass someInstance;
  
      public SomeOtherClass() {
          someInstance = new SomeClass();
          someInstance.someEvent.addEventHandler(this::someEventHandler);
      }
  
      private void someEventHandler(Object sender, SomeEventArgs args) {
           // do some stuff ...
      }
  }
  </code></pre>
  
  <p>To me this seems fine because I am not exposing any implementation details, actually the event is part of the interface of the class.
  Am I completely wrong on this? Shoud I still use a getter to interact with the event?</p>
  
  <p>Another example that I can think of is a logger that is used in the class but can be set from the outside to get a certain level of detail.</p>
  
  <p>So, to extend the question, is it ok to use a public modifier if the field is a third party component that can be accessed to achieve a specific behaviour? </p>