Sorry, but I've become sick of using the keyword 'final' all over the place so I figured that I only have to use it once and then I can delegate an object anywhere and everywhere throughout my program, without the worry of the "new" keyword.
Or so it may seem.http://members.optusnet.com.au/ksaho/something/test.jar
Source code is in the jar file as well.
I will explain what's going on.I have created 5 classes.The main class creates an object called "ObjA" with an index value of 1.
The main class creates an object called "DelegateA" after ObjA is created.
ObjA is called "obj" and it's a "final"(immutable) object, and is sent into DelegateA's constructor.
DelegateA creates a new object called DelegateB and send obj into DelegateB's constructor.
DelegateB creates an object called "Modd" and send obj into it's contructor.
Modd then proceeds to reinstance obj by calling "obj = new ObjA(-1);".
The change is recognised within the Modd object but isn't recognised within Main.
However the same behaviour occurs wheather or not the object is declared immutable within the Main class.
Sorry if you guys have discussed this 10 years ago, but I've just found this out.
Personally I see this as a problem because if an object is declared immutable, it should remain immutable wherever it may go.
The current behaviour does not do this, and can introduce bugs and incompatibilities into your application.
If you wrote your app to be extensible and a class passes an immutable object, the mere fact that you can create a new instance from it can cause issues within your program.
The second issue is that even if a superclass declares the object to be immutable in it's constructor, a subclass doesn't recognise this and makes an assumption that the object is mutable.
Either Sun has created a giant boom boom, or I may have created an error in my program and a lack of caffeine has caused me to jump one ship too far.
Also I just want to point out in Sun's words, what an immutable object or vairable is.
I've also tested my program with primitive types and the same behaviour occurs.http://java.sun.com/docs/books/tutorial/java/nutsandbolts/finalVariables.html
You can declare a variable in any scope to be final (in the glossary). The value of a final variable cannot change after it has been initialized. Such variables are similar to constants in other programming languages.