Can I create a class that instantiates with just the =
operator, like the String
class does? Or is that a feature specific to String
class in Java?
-
6No, you can't. It's something that, as you said, `lies inside Java`. – u32i64 Aug 14 '17 at 07:05
-
12And Strings are *not* instantiated with `=`. String constants are defined with `"..."` and it only gets more complicated from there. So, no, you cannot. Use `new`. If you do not like `new`, you can hide it behind a static factory method or something like that. – Florian Schaetz Aug 14 '17 at 07:08
-
https://stackoverflow.com/questions/2068570/how-to-create-a-string-class-replica – soorapadman Aug 14 '17 at 07:09
-
I meant If I Can I tell Java what to do if there is "=" next to my class or "+=". I gave String class just an example of this. Not internals of String class. I get answer by replies with user-defined Operator Overloading. Thanks all. – matthew Aug 14 '17 at 10:16
-
1@matthew - Java doesn't actually do anything special for "=" with strings (it does for "+=", but only because it has special behaviour for "+"). – Jules Aug 14 '17 at 17:46
-
If you are interested in this I highly recommend playing around with Groovy. You can run 100% java classes through groovy (it's nearly a perfect superset) but it also allows a lot of new syntax such as operator overloading. I prefer Java for production code, but Groovy is a lot of fun to mess around with. – Bill K Aug 14 '17 at 20:26
3 Answers
No, you can't create a class that's instantiated just with =
operator because you can't overload an operator in Java like you can in C++ or C# (see Operator overloading in Java).
String
s are instantiated when you use "something"
only if they do not already exist in memory, so you get a reference to the same exact String
object each time you write "something"
.
For example, if you do:
String a = "something";
String b = "something";
Then
a == b; // will be true.
You can take a look at these questions to learn more about how String
objects work:

- 2,384
- 3
- 22
- 36
Because Java does not support user-defined Operator Overloading a new instance can not be created with =
Operator.
Check out Why doesn't Java offer operator overloading? for more information

- 704
- 5
- 26
Code String s = "Hello World!"
does not create a new String
. It assigns a reference of a String
existing in String Pool to s
. If the String
does not exist in String Pool, then a new String
object is created in String Pool, but not with the operator =
all alone.
This creates new String
objecs:
String s1 = new String("Hello World!"); // new Object
String s2 = new String("Hello World!"); // new Object
System.out.println(s1 == s2); // false
This may or may not create a new String
object in String Pool:
String s1 = "Hello World!";
String s2 = "Hello World!";
System.out.println(s1 == s2); // true
You could get fairly close to the behaviour mentioned above with using getInstance()
pattern, consider this:
public class Singleton {
private Singleton(){}
private static class SingletonHelper{
private static final instance INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHelper.INSTANCE;
}
}
Then you could use:
Singleton s = Singleton.getInstance();

- 91
- 3
-
3The first code snippet does not necessarily create a new `String` in the string pool (the strings created by `new String(...)` are not contained in the pool). – Nevay Aug 14 '17 at 15:43
-