Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>If you are asking how to access the <code>HashMap</code>s inside your <code>Data</code> object</strong></p> <p>To access one of the three HashSets, you would create a new Data object and access the public field of choice after the Data object has been initialized:</p> <pre><code>HashSet&lt;String&gt; setOne = new HashSet&lt;String&gt;(); HashSet&lt;String&gt; setTwo = new HashSet&lt;String&gt;(); HashSet&lt;String&gt; setThree = new HashSet&lt;String&gt;(); Data d = new Data(setOne, setTwo, setThree); d.Blue.add("this will be added to setTwo"); </code></pre> <p>Normally however, you would want not to have public fields, but instead make them private and provide getter/setter methods, as this will provide <a href="http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29" rel="nofollow">encapsulation</a>.</p> <p><strong>If you are asking whether you should refer to the interface or class</strong></p> <p>First: <em>HashSet is a concrete class and Set is an interface.</em> Also: <em>The reference type decides what methods you can call upon an object. (This would be the left side of the equals sign, while the right side is the dynamic type).</em></p> <p>In the constructor you would want to be the least specific as possible. Are you not using any special functionality of the HashSet class, you should favour referring to the Set interface, as this is much more flexible should you want to change implementation of the Data class later on - it also makes it possible for users of your class to use every class that implements Set rather than to be restricted of using the HashSet.</p> <p>Do you however need special functionality of a specific class, you would of course then refer to that class.</p>
 

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