Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a more apposite example. (Based on @Andreas_D's example ... as if it ain't obvious!)</p> <p>Note that an import is <strong>not</strong> needed to refer to a class declared in the same package.</p> <pre><code>package pkg; public class One { public int fieldOne; } </code></pre> <hr> <pre><code>package pkg; public class Two { private One one; public int getOne() { return one.fieldOne; } } </code></pre> <hr> <p>Having said this, referring to public attributes declared in another class is generally a bad idea. It is better to declare the attributes as private and access them via getter and setter methods. (If the attributes are exposed as public and they are not final, that anything can modify them. Furthermore, there is no way that a subclass can hide the attributes, or change the way that they behave.)</p> <p><strong>EDIT</strong></p> <p>To illustrate the last point:</p> <pre><code>public class A { public int counter; private int counter2; public int getCounter2() { return counter2; } public void setCounter2(int c) { counter2 = c; } } public class B extends A { public void setCounter2(int c) { if (c &lt; 0) throw new IllegalArgumentException("counter2 cannot be negative"); super.setCounter2(c); } } </code></pre> <p>Compare the way that the <code>counter</code> and <code>counter2</code> attributes behave. In the case of <code>counter</code>, any code in any class can read or update the attribute without restriction. This applies both for instances of the class <code>A</code> and any classes that extend <code>A</code>. If some other class decides to set <code>counter</code> to a bad value ... there's nothing to prevent it.</p> <p>In the case of <code>counter2</code>, the attribute of an instance of <code>A</code> can only be read or updated by the <code>getCounter2</code> and <code>setCounter2</code> methods respectively. If I want to, I can change these methods in <code>A</code> to do validation, perform access checks, notify some listener of a state change an so on. And because this behavior can only occur in methods of <code>A</code>, I know where to start looking in the event of something strange happening. </p> <p>If I define a subclass of <code>A</code> (e.g. <code>B</code>), I can <strong>override</strong> either or both of the methods to do something different. But note that since I decided to declare the actual attribute as <code>private</code>, the <code>setCounter2</code> method in B still has to call the method in its superclass. And that's good too.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload