Do we have a Readonly field in java (which is set-able within the scope of the class itself)?
<p>Hi all I was wondering is there anyway to set a field "writable" only within the class but "readable" throughout the whole package?</p>

<p>This is the current code:</p>

<pre><code>Class C {
    private int width, height;

    int GetWidth(){
        return width;
    }

    int GetHeight(){
        return height;
    }
    //..
</code></pre>

<p>But I'm using this code in some places (painting) whereby I would not like the function overhead while reading (from what I know the compiler does not optimize this kind of code).</p>

<p>I could simply go with:</p>

<pre><code>Class C {
    int width, height;
    //..
</code></pre>

<p>But this does not stop accidental modification of the variables from other package-private classes.</p>

<p><code>final</code> doesn't do the trick of course (otherwise I couldn't even modify the variable within the class itself). What I want is something like this:</p>

<pre><code>Class C {
    readonly int width, height; // only privately settable
    //..
</code></pre>

<p>I was wondering what's the best solution?</p>

<p><sub>PS: The above code is an example. I'm not optimizing anything here. I'm wondering if Java grammar has a way to  set a field "writable" only within the class but "readable" throughout the whole package.</sub></p>
