🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #14461

Re: How to make a "constant" object

#14461
From: Steve Horsley
Date: Wed, 01 Sep 1999 00:00
64 lines
2859 bytes
If you don't ever want to change the cnotents, you can do something 
like:

public class ABC {
    public final int iField;
    public ABC() { ifield = 0; }
    public ABC(int i) { ifield = i; }
}

This is an immutable object. The value is set in the constructor
and cannot be changed by anyone afterwards. iField can be read
by anyone, though.

If you think this is restrictive, think about the String class.
Strings are immutable - and operation like String.append() returns a
new string - the original remains unchanged. This is very handy.
If you have a reference to a string, you know that no other thread
can f*** with it, even if they are appending, replacing etc. 
Equally, you can use the String modifying functions without having
to worry about upsetting other code that still refers to the 
original. It's easy to forget this when you do things like
    myString = myString.replace('A', 'B');
but I am very happy that Strings are immutable.

Steve.
//-------------------------
Michael Drews wrote:
> 
> I am fairly new to Java, having used C++ in the past. I would like to
> know if there is a way (or what is the best way) to protect the data in
> a Java object when needed. Consider the following class:
> 
> public class ABC
> {
>     int iField = 0;
>     public ABC() {}
>     public setField(int i) { iField = i; }
> }
> 
> In C++, I could declare a constant variable of class ABC:
> 
>     const ABC abc = new ABC();
> 
> and then, if I tried either of the following statements, the compiler
> would generate an error, since I was trying to change the constant
> object or its data:
> 
>     abc = new ABC();        // Error
>     abc.setField(2);            // Error
> 
> In Java, if I declare a final variable, I can't change the object which
> the variable references, but I can change the data in the referenced
> object:
> 
>     final ABC abc = new ABC();
>     abc = new ABC();        // Error
>     abc.setField(2);            // This is allowed!
> 
> I want to be able to declare some sort of "final" or "constant" object
> which does not allow its data to be modified. Is there any mechanism for
> this in Java, or some accepted method for accomplishing this task?
> Thanks in advance.
> 
> Mike Drews
Technical Details
Message-ID: <37CCF441.FF402C2F@cwcom.co.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <37C5BCCC.8F102F85@pe-nelson.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,99c34274d2b6c246
X-Google-Attributes: gidf74b3,public
From: Steve Horsley <steve.horsley@cwcom.co.uk>
Subject: Re: How to make a "constant" object
Date: 1999/09/01
Message-ID: <37CCF441.FF402C2F@cwcom.co.uk>
X-Deja-AN: 519668914
Content-Transfer-Encoding: 7bit
References: <37C5BCCC.8F102F85@pe-nelson.com>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: Cable & Wireless Communications
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language