I'm not 100% sure, but I don't think you can. I believe that the String class gets special treatment by the compiler to allow that.
Now, I guess you COULD implement your own compiler that would give you this support for your class, but it's probably not worth it.
Never ascribe to malice that which can be adequately explained by stupidity.
Joined: Oct 13, 2005
And welcome to JavaRanch
Joined: Nov 24, 2009
malaya nayak wrote:how can i create my own class that can be instantiated like we are creating string objects..
String s= "abc";
MyClass c= "xyz";// here my object should get initialized....
It's not possible in Java. The reason it works for String is because this particular class gets special language support to behave partly like a primitive.
You can come quite close though. Instead of using new you use a static creation class for your objects. And if you use a so called static import you don't need to prefix static methods of a class. The end-result would be something like,
MyClass c= create("xyz");
It's not perfect but it's as close as Java will take you at the moment.