Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
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 839 times)
0 Members and 1 Guest are viewing this topic.
Offline K.I.L.E.R

Senior Member




Java games rock!


« Posted 2005-04-17 03: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.
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. 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.

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/finalVariables.html
Quote

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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


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

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

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #2 - Posted 2005-04-17 09: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 :-

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public static void doesNothing(String s)
{
   s = "b";
}

public static void main(String [] args)
{
   final String str = "a";
   System.out.println(str);
   doesNothing(str);
   System.out.println(str);
}


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().

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline K.I.L.E.R

Senior Member




Java games rock!


« Reply #3 - Posted 2005-04-17 11: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.

Vorax:
Is there a name for a "redneck" programmer?

Jeff:
Unemployed. Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org 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‑gaming.org
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!