Why shouldn't I be using public variables in my Java class?
<p>In school, I've been told many times to stop using <code>public</code> for my variables. I haven't asked why yet.</p>

<p>This question: <a href="http://programmers.stackexchange.com/questions/50225/are-javas-public-fields-just-a-tragic-historical-design-flaw-at-this-point">Are Java&#39;s public fields just a tragic historical design flaw at this point?</a> seems kinda related to this. However, they don't seem to discuss <em>why</em> is it "wrong", but instead focus on <em>how</em> can they use them instead.</p>

<p>Look at this (unfinished) class:</p>

<pre><code>public class Reporte {

    public String rutaOriginal;
    public String rutaNueva;
    public int bytesOriginales;
    public int bytesFinales;
    public float ganancia;

    /**
     * Constructor para objetos de la clase Reporte
     */
    public Reporte() {

    }
}
</code></pre>

<p>No need to understand Spanish. All this class does is hold some statistics (those public fields) and then do some operations with them (later).</p>

<p>I will also need to be modifying those variables often. But well, since I've been told not to use <code>public</code>, this is what I ended up doing:</p>

<pre><code>public class Reporte {

    private String rutaOriginal;
    private String rutaNueva;
    private int bytesOriginales;
    private int bytesFinales;
    private float ganancia;

    /**
     * Constructor para objetos de la clase Reporte
     */
    public Reporte() {

    }

    public String getRutaOriginal() {
        return rutaOriginal;
    }

    public String getRutaNueva() {
        return rutaNueva;
    }

    public int getBytesOriginales() {
        return bytesOriginales;
    }

    public int getBytesFinales() {
        return bytesFinales;
    }

    public float getGanancia() {
        return ganancia;
    }

    public void setRutaOriginal(String rutaOriginal) {
        this.rutaOriginal = rutaOriginal;
    }

    public void setRutaNueva(String rutaNueva) {
        this.rutaNueva = rutaNueva;
    }

    public void setBytesOriginales(int bytesOriginales) {
        this.bytesOriginales = bytesOriginales;
    }

    public void setBytesFinales(int bytesFinales) {
        this.bytesFinales = bytesFinales;
    }

    public void setGanancia(float ganancia) {
        this.ganancia = ganancia;
    }
}
</code></pre>

<p>Looks kinda pretty. But seems like a waste of time.</p>

<p>Google searches about "When to use public in Java" and "Why shouldn't I use public in Java" seem to discuss about a concept of <strong>mutability</strong>, although I'm not really sure how to interpret such discussions. I do want my class to be mutable - all the time.</p>
