Search code examples
javaexpressioninstanceinitializer

Java objects without creating instance


I recently started learning java and i am trying to understand How is it possible to equalize a reference object to a variable or method without creating instance, like

Example:

class A {

    View  v; 
    v = [someMethod();/variable;]
    // not necessarily view class
}

Solution

  • View w = <expression>;
    

    where <expression> may create a new instance of View or may be evaluated to a reference to the object that already been created at some point in the past.

    For instance,

    Integer i = Integer.valueOf(0);
    

    returns the same object for 0 that was created when the Integer class was loaded into the JVM.