Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't really have a generic getter if you don't know what you want to get, for example :</p> <pre><code>boolean myNewBool= get("myString1"); </code></pre> <p>If <code>get</code> returns something, but you don't really know if this something is compatible with a boolean, and terrible things could happen.</p> <p>You could try this:</p> <pre><code>public &lt;T&gt; get(String element){ return (T) elementToGet; } </code></pre> <p>But you would have to specify the return type when you call the get method.</p> <pre><code>String element = myObject.&lt;String&gt;get("element"); </code></pre> <p>Here are the bad sides :</p> <ul> <li>You can't work directly with primitives</li> <li>You can have a lot of ClassCastException</li> <li>If you misspell an attribute name you won't see it until you run it</li> <li>You don't expose a nice public API, people would have to know evert possible attribute to use it, and as said above, a misspelled attribute (or an inexistant one) wouldn't be seen until runtime.</li> <li>You have to know the return time and type it each time you use your method</li> <li>You would have to type a really long (and smelly) code in your <code>get</code> method either to use each possible attribute (if you still want have some private and not accessible) or worse, use reflection to find the right attribute.</li> </ul> <p>So definitively <strong>not a good idea</strong>.</p> <p>What you can do instead is using the good old getters//setters and if there is a lot of them, generate them with your IDE.</p> <p>Another way would be to use the project lombok.</p> <hr> <p><strong>Resources :</strong></p> <ul> <li><a href="http://projectlombok.org/" rel="nofollow noreferrer">Project Lombok</a></li> </ul> <p><strong>On the same topic :</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/2707912/create-automatically-only-getters-in-eclipse">Create automatically only getters in Eclipse</a></li> <li><a href="https://stackoverflow.com/questions/2252914/eclipse-generate-all-getters-setters-in-package">Eclipse Generate All getters setters in package</a></li> <li><a href="https://stackoverflow.com/questions/1907312/java-getters-and-setters">Java Getters and Setters</a></li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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