Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

final array

Thread view: 
josh - 23 Oct 2006 13:14 GMT
Hi, if I wanted that in a method an argument cannot change its
reference or cannot
change its array elemets how does it?

i.e.

public static void passMe(int a[])
{
    a[1] *= 10;

       // or
      // a = new int[3];  I don't want .....
}

public static void main(String[] args)
{
    int x[] = {42,22,33};

    System.out.println("x[1] before: " + x[1]);

    passMe(x);

    System.out.println("x[1] after: " + x[1]);
}

here in the example x[1] value is changed.....
Chris Brat - 23 Oct 2006 13:38 GMT
Hi,

set the methods array parameter as final so that its reference cant
change and
create a copy of the array to work with so that the original values
dont change.

Chris
Hendrik Maryns - 23 Oct 2006 14:52 GMT
josh schreef:
> Hi, if I wanted that in a method an argument cannot change its
> reference or cannot
[quoted text clipped - 3 lines]
>
> public static void passMe(int a[])

In Java, it is customary to use the syntax int[] a.  int a[] is a C
heritage.

> {
>     a[1] *= 10;
>
>         // or
>        // a = new int[3];  I don't want .....
> }

You are confusing two things: changing the value of what a is
referencing, or changing the reference.  Arrays are objects, so you
cannot forbid anyone to change its state (i.e. change its elements).
You can, however, claim that you aren’t going to change the reference,
by doing

public static void passMe(final int[] a)

but the use of this is questionable, since it is still possible to
change the elements of a.

> public static void main(String[] args)
> {
[quoted text clipped - 8 lines]
>
> here in the example x[1] value is changed.....

Then pass a copy of x.  If you don’t want others to meddle with your
objects, then don’t pass them to them.

passMe(x.clone())

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Oliver Wong - 23 Oct 2006 15:40 GMT
> Then pass a copy of x.  If you don't want others to meddle with your
> objects, then don't pass them to them.
>
> passMe(x.clone())

   Alternatively, create a new class which wraps around your mutable
object, and only expose read-only methods.

passMe(new ReadOnlyArray(x));

   Alternatively, document the method as not modifying its parameter, so
that implementors know not to modify the parameters. This assumes you're not
working with other implementors who might be malicious (i.e. it assumes
you're not writing some sort of security tool)

/**
* Does something. Does NOT modify a.
*/
public void passMe(int[] a) {
}

   - Oliver
Robert Klemme - 23 Oct 2006 21:36 GMT
>> Then pass a copy of x.  If you don't want others to meddle with your
>> objects, then don't pass them to them.
[quoted text clipped - 3 lines]
>     Alternatively, create a new class which wraps around your mutable
> object, and only expose read-only methods.

Actually, he needs not create a new class.  This should do the job (from
memory):

Collections.unmodifiableList(Arrays.asList(foo))

Kind regards

    robert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.