I have a very basic doubt. Is it possible to create a class in which objects can be created using '=' operator (just like Integer/String class) rather than using "new CustomClass()".
Consider a class named CustomClass. I need to define my class just a way that it works just like Integer/String class in terms of the syntax for create objects.
Eg. In order to create Integer object we write, Integer pInt = 10 or String pStr = "testWorld"; LikeWise, I want objects of my custom class "CustomClass" to be created using simple syntax like CustomClass custObj = 45;
No, Java doesn't provide a way for that. Even String
is a special case in Java, given some extra syntax to make things easier. As for Integer x = 10;
, that's just autoboxing although it doesn't require the new
syntax either.
You'll have to make do with constructors and factory methods.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments