javastringobjectinstantiation

How java create objects implicitly? Like in case of String class


I can't understand how an object is created implicitly.

Example:

String s = "implicit instantiation";

Can I make my own class whose objects can be created implicitly?


Solution

  • No, String instantiation is handled implicitly by the compiler. Only the String and Array classes have this property.

    String greeting = "Hello world!";
    char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
    

    Autoboxing allows you to implicitly instantiate objects of primitive wrapper types, but that's also a special case handled by the compiler. You can't create your own classes with this ability.

    Boolean b = false;
    Integer i = 0;
    Double pi = 3.1416;