toton wrote:
> I get paid for C++
My condolences
> That says that the list can not be
> modified. However I want the list element should not be able to
> modified Just like const_iterator in C++.
You won't get a const_itertator. Java is not C++.
a) You will hopefully soon find out that it is typically a waste of time
trying to protect objects from getting modified. If you have designed
your objects in the right way (they manage to keep themselves
consistent), it should be perfectly ok to modify an object. Totally
independent of who does it. If you don't trust someone with your
objects, don't give them the objects at all. If you trust them, let them
do whatever they want.
> As the List returns a get
> reference to object, the object always can be modified.
b) No, not always. Only objects which allow others to modify them can be
modified. If you want objects which can't be modified, add only objects
to the list which don't provide methods for modification (e.g. immutable
objects). What? Your objects provide methods for modification? Well,
then wrap them in some immutable wrapper object before you add them to
the list. Typically, however, that is a waste of time and resources.
public class ValuableObject {
public int getData() { ...}
public void setData(int data) { ... }
}
public class ReallyValuableProtectedObject {
private ValuableObject vo;
public ReallyValuableProtectedObject(ValuableObject vo) {
this.vo = vo;
}
public int getData() { return vo.getData(); }
// do not implement setData() or implement
// it as a no-op
}
You can decorate all this with the usage of some common interface, e.g.:
interface ReadableValuableObject {
public int getData();
}
public class ValuableObject implements ReadableValuableObject { ... }
public class ReallyValuableProtectedObject implements
ReadableValuableObject { ... }
And when you have done that a few hundred times you might recognize that
you are wasting your time.
/Thomas
--
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hie...lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq