Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java's immutability isn't solid  (Read 1574 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Devvie

Java games rock!

« Posted 2005-04-17 01:15:27 »

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.

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. Smiley

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.

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.

Is there a name for a "redneck" programmer?

Unemployed. Wink
Offline ryanm

Senior Devvie

Projects: 1
Exp: 15 years

Used to be bleb

« Reply #1 - Posted 2005-04-17 07:39:17 »

Remember that Java passes all arguments by value. For objects, the value of the reference is passed.
Online Abuse

JGO Ninja

Medals: 66

falling into the abyss of reality

« Reply #2 - Posted 2005-04-17 07:40:00 »

errrrr, the behaviour your program demonstrates is in perfect accordance with the Java spec.

When you pass an Object as a parameter, all you are passng is a copy of the reference to that Object. (not a pointer to the reference, or the Object itself)

Therefor a method which alters where any of its parameters point, will have no effect on any other references.

Simplifying your example :-

public static void doesNothing(String s)
   s = "b";

public static void main(String [] args)
   final String str = "a";

The above code will print "a" twice, as a copy of the reference is passed into doesNothing(), so reassigning it will have no effect in main().
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline K.I.L.E.R

Senior Devvie

Java games rock!

« Reply #3 - Posted 2005-04-17 09:20:44 »

Son-ov-a.... Damn, I forgot, this isn't C.

I can't believe I made a mistake like this after nearly 2 years of Java programming experience.

Sometimes I should save myself the humilitation and not get out of bed. Roll Eyes

Thanks for clearing this up guys.

Is there a name for a "redneck" programmer?

Unemployed. Wink
Pages: [1]
  ignore  |  Print  

EgonOlsen (77 views)
2018-06-10 19:43:48

EgonOlsen (57 views)
2018-06-10 19:43:44

EgonOlsen (77 views)
2018-06-10 19:43:20

DesertCoockie (259 views)
2018-05-13 18:23:11

nelsongames (157 views)
2018-04-24 18:15:36

nelsongames (156 views)
2018-04-24 18:14:32

ivj94 (897 views)
2018-03-24 14:47:39

ivj94 (161 views)
2018-03-24 14:46:31

ivj94 (810 views)
2018-03-24 14:43:53

Solater (174 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!