You could create multiple instances by:
- Executing the same block of code more than once.
- Cloning an instance of an anonymous class.
Question
An anonymous class is something like this:
SenseOfLife _42 = new SenseOfLife() {
public int eval() {
return 42;
}
};
I didn't understand why such an anonymous class can have an unbounded number of instances (this is a proposition).
While I'm defining this anonymous class it creates automatically an instance and assigns it to the variable _42
. So I have only one instance and cannot create a new instance.
Solution
You could create multiple instances by:
OTHER TIPS
If the interface extends Cloneable
the class could technically be cloned.
Reflection could get a new instance.
If the same new SenseOfLife()
constructor is called in a loop that constructor will get compiled down to one class that is instantiated multiple times.
I haven't tried it, but most likely you can create other instances using reflection, i.e., instance.getClass().newInstance()
.