Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Method names should be verbs, not nouns.</p> <p>So if you want something generic, I'd just go with <code>wrap()</code></p> <blockquote> <h2>Naming a Method</h2> <p>Although a method name can be any legal identifier, code conventions restrict method names. <strong>By convention, method names should be a verb in lowercase or a multi-word name that begins with a verb in lowercase, followed by adjectives, nouns, etc.</strong> In multi-word names, the first letter of each of the second and following words should be capitalized. Here are some examples:<br> <code>run</code><br> <code>runFast</code><br> <code>getBackground</code><br> <code>getFinalData</code><br> <code>compareTo</code><br> <code>setX</code><br> <code>isEmpty</code></p> </blockquote> <p>Source: <a href="http://download.oracle.com/javase/tutorial" rel="nofollow">The Java&trade; Tutorials</a> > <a href="http://download.oracle.com/javase/tutorial/java" rel="nofollow">Learning the Java language</a> > <a href="http://download.oracle.com/javase/tutorial/java/javaOO/classes.html" rel="nofollow">Classes</a> > <a href="http://download.oracle.com/javase/tutorial/java/javaOO/methods.html" rel="nofollow">Defining Methods</a></p> <hr> <p>And about reusing method names. It works, but it requires some care. Here's an example:</p> <p><strong>An object</strong> with many constructors:</p> <pre><code>public class Wrapper{ private final String type; public Wrapper(final byte inner){ type = "byte"; } public Wrapper(final short inner){ type = "short"; } public Wrapper(final int inner){ type = "int"; } public Wrapper(final char inner){ type = "char"; } public Wrapper(final Object inner){ type = "Object"; } public Wrapper(final float inner){ type = "float"; } public Wrapper(final double inner){ type = "double"; } public Wrapper(final boolean inner){ type = "boolean"; } public String getType(){ return type; } } </code></pre> <p><strong>Some factory methods</strong> to get such objects:</p> <pre><code>public static Wrapper wrap(final byte inner){ return new Wrapper(inner); } public static Wrapper wrap(final int inner){ return new Wrapper(inner); } public static Wrapper wrap(final short inner){ return new Wrapper(inner); } public static Wrapper wrap(final Object inner){ return new Wrapper(inner); } public static Wrapper wrap(final boolean inner){ return new Wrapper(inner); } public static Wrapper wrap(final char inner){ return new Wrapper(inner); } public static Wrapper wrap(final float inner){ return new Wrapper(inner); } public static Wrapper wrap(final double inner){ return new Wrapper(inner); } </code></pre> <p><strong>Test code:</strong></p> <pre><code>public static void main(final String[] args){ final byte byteValue = (byte) 0x12; final short shortValue = (short) 10; System.out.println(wrap(byteValue).getType()); System.out.println(wrap(10).getType()); System.out.println(wrap(shortValue).getType()); System.out.println(wrap("").getType()); System.out.println(wrap(true).getType()); System.out.println(wrap('a').getType()); System.out.println(wrap(13.1f).getType()); System.out.println(wrap(13.1d).getType()); } </code></pre> <p><strong>Output (as expected):</strong></p> <blockquote> <p>byte<br> int<br> short<br> Object<br> boolean<br> char<br> float<br> double</p> </blockquote>
    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. 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