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?
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?
For every instance of an object you need a Constructor and a constructor its a a special method for construct and initialize methods. Example:
So how do you construct a new object in java? Easy with the
new operator
you create a New Object!But here is something that a lot of newbies get confussed. Why i can do this:
Because
String
is a special case in Java and you don't need to add anew operator
like all the primitive variables that are classes. Example:and So on.
No, String instantiation is handled implicitly by the compiler. Only the String and Array classes have this property.
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.
Unfortunately you just can not do that!
opposite to C or C++ you can not overload any operator in java language, so there is no possible way to do something like
in the case of the string class:
that is sugar sintax for the developers, behind the scenes is the compiler doing the "dirty" work and doing something like (remember there is a string pool):
The same applies for some other Types like Arrays, or wrapper for numbers...