Java : Why Do Want to New Different Objects From One Class

I was asked this once. Why do we need to new object every time we want to use it.
Yes, my first answer is, we need different objects because we need to use them separately because they are on different locations in Heap memory. However, this is not actually the answer. So, why do we need to new object every time we want to use it.

For instance,

1
2
A a = new A();
A a1 = new A();

What’s the different between a and a1. They behave exactly the same, because they are A class. The reason is we would like to actually change their attributes or properties inside the class. This is very fundamental Object-oriented principle.

Nov 21st, 2009

Comments