Can I write a literal initializer in Java for a non-primitive type?
<p>Java's literal initializers are mostly used for primitives (e.g. <code>int i = 2</code> and <code>double j = 4.2</code>), but there are certain non-primitive classes that also have literal initializers (e.g. <code>String a = "Hello"</code> or <code>Number z = 43</code>). I am looking to implement an initializer similar to that in a class that I wrote. I have a class <code>Numeric</code> that extends <code>Number</code>, that I would like to be able to initialize as <code>Numeric a = 43</code>. Is there any way that this can be done in Java, and if so, how?</p>

<p>Here is the part of the source code for <code>Numeric</code></p>

<pre><code>public class Numeric extends Number {

    private HashMap&lt;Primitive, Number&gt; values;
    private Primitive origin;

    public Numeric(byte value) {
        values = new HashMap&lt;&gt;();
        values.put(Primitive.BYTE, value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.BYTE;
    }

    public Numeric(short value) {
        values = new HashMap&lt;&gt;();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.SHORT;
    }

    public Numeric(int value) {
        values = new HashMap&lt;&gt;();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.INT;
    }

    public Numeric(long value) {
        values = new HashMap&lt;&gt;();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.LONG;
    }

    public Numeric(float value) {
        values = new HashMap&lt;&gt;();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, value);
        values.put(Primitive.DOUBLE, (double) value);
        origin = Primitive.FLOAT;
    }

    public Numeric(double value) {
        values = new HashMap&lt;&gt;();
        values.put(Primitive.BYTE, (byte) value);
        values.put(Primitive.SHORT, (short) value);
        values.put(Primitive.INT, (int) value);
        values.put(Primitive.LONG, (long) value);
        values.put(Primitive.FLOAT, (float) value);
        values.put(Primitive.DOUBLE, value);
        origin = Primitive.DOUBLE;
    }
</code></pre>

<p><code>Primitive</code> is just a basic <code>enum</code> with all the primitive types. </p>