Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > inner class loses track of int[] variable

Reply
Thread Tools

inner class loses track of int[] variable

 
 
Composer
Guest
Posts: n/a
 
      02-23-2009
My class has an inner class as follows (irrelevant parts omitted):

class ResultInstance
{ private int[] s;

// Constructor method
public ResultInstance(int[] series)
{ s = series;
System.out.println(PitchClassSet.toString(s)); // At
this point it's fine !!!
}

protected int[] getSeries()
{ System.out.println(PitchClassSet.toString(s)); // By now
it's been screwed up !!!
return s;
}
}

At the point where the main class creates a new ResultInstance, I can
verify that the array "s" is fine.
All the ResultInstances are put into a Vector. Later I pull them out
using an Enumeration.
At that point, some of the "s" arrays have been replaced with those
that belong to later ResultInstances.

No doubt I'm doing something stupid. Can someone help me spot it?
Thanks.
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-23-2009
Composer wrote:
> My class has an inner class as follows (irrelevant parts omitted):


You've also omitted some very relevant parts, like the definition of
'PitchClassSet'. You also omitted what's being done to the array to which 's'
points.

> class ResultInstance


Your gaps are far too wide for Usenet readability.

> { private int[] s;
>
> // Constructor method
> public ResultInstance(int[] series)
> { s = series;
> System.out.println(PitchClassSet.toString(s)); // At
> this point it's fine !!!
> }
>
> protected int[] getSeries()
> { System.out.println(PitchClassSet.toString(s)); // By now
> it's been screwed up !!!
> return s;
> }
> }
>
> At the point where the main class creates a new ResultInstance, I can
> verify that the array "s" is fine.


How is the input 'series' used by the class that uses 'ResultInstance'?

> All the ResultInstances are put into a Vector. Later I pull them out
> using an Enumeration.
> At that point, some of the "s" arrays have been replaced with those
> that belong to later ResultInstances.
>
> No doubt I'm doing something stupid. Can someone help me spot it?


First thing you shouldn't be doing, though it's not the reason for the problem
you see, is using 'Vector' or 'Enumeration'. Those classes were replaced in
1998, for Pete's sake! Use 'ArrayList' and 'Iterator'.

The problem you're seeing is that all 'ResultInstance' instances are using the
same array. Every 'ResultInstance' instance's 's' is pointing to the same array.

--
Lew
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      02-23-2009
On Sun, 22 Feb 2009 20:26:23 -0800 (PST), Composer
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

> System.out.println(PitchClassSet.toString(s)); // By now
>it's been screwed up !!!
> return s;


I suggest you run your code in a debugger and trace the code, watching
to see who buggers up s.
Check your code for patterns like s = ????.
That code is likely being executed between your constructor and your
getSeries.
--
Roedy Green Canadian Mind Products
http://mindprod.com

One path leads to despair and utter hopelessness. The other,
to total extinction. Let us pray we have the wisdom to choose correctly.
~ Woody Allen .
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      02-23-2009
Composer wrote:
> My class has an inner class as follows (irrelevant parts omitted):
>
> class ResultInstance
> { private int[] s;
>
> // Constructor method
> public ResultInstance(int[] series)
> { s = series;
> System.out.println(PitchClassSet.toString(s)); // At
> this point it's fine !!!
> }


"s" here is not very private. You just copy the reference, not the
whole array. Thus, after passing "series" into ResultInstances, if
anyone then later changes "series", "s" will get changed too.

You might want to do this:

s = series.clone();

which will clone the array for you, thus giving you a separate, private
copy.

Aside from that silly wild guess, Lew's right, you haven't given us
nearly enough info to determine what has gone wrong. The getSeries()
method also returns a reference, not a copy, which allows anyone to
wreak havoc with your supposed "private" variable. Try cloning that
too, see if the problems go away.




 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-23-2009
Mark Space wrote:
> "s" here is not very private. You just copy the reference, not the
> whole array. Thus, after passing "series" into ResultInstances, if
> anyone then later changes "series", "s" will get changed too.
>
> You might want to do this:
>
> s = series.clone();
>
> which will clone the array for you, thus giving you a separate, private
> copy.
>
> Aside from that silly wild guess, Lew's right, you haven't given us
> nearly enough info to determine what has gone wrong. The getSeries()
> method also returns a reference, not a copy, which allows anyone to
> wreak havoc with your supposed "private" variable. Try cloning that
> too, see if the problems go away.


Be careful - clone() makes a shallow copy. While the array won't change after
implementing this fix, individual elements might.

--
Lew
 
Reply With Quote
 
Lothar Kimmeringer
Guest
Posts: n/a
 
      02-23-2009
Lew wrote:

> Be careful - clone() makes a shallow copy. While the array won't change after
> implementing this fix, individual elements might.


Generally speaking, yes, but we are talking about an int[] here,
where the copy is "deep" (in difference to an int[][]...)


Regards, Lothar
--
Lothar Kimmeringer E-Mail: http://www.velocityreviews.com/forums/(E-Mail Removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
Reply With Quote
 
Composer
Guest
Posts: n/a
 
      02-23-2009
Many thanks to all, especially to Mark Space who led me straight to a
resolution.

I hadn't known about the clone() method of arrays.

Nor had I known that "s = series" simply copied a reference from a
different class.

I'm happy to learn things like this. Sorry to have troubled anyone in
the process.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-23-2009
On Feb 23, 2:02*pm, Composer <(E-Mail Removed)> wrote:
> Many thanks to all, especially to Mark Space who led me straight to a
> resolution.
>
> I hadn't known about the clone() method of arrays.
>


'clone()' is a protected method of all Objects, except for those for
which it has been elevated to 'public' access such as arrays.

> Nor had I known that "s = series" simply copied a reference from a
> different class.
>


That is a truism for all reference variables, except it's not "from a
different class". When you assign any reference to a reference
variable, it is the pointer that gets copied, not the instance to
which it points.

In order for the reference assignment to succeed, far from being "from
a different class", the type of the source variable must be assignment-
compatible with that of the destination variable. That means that
they must either be of the same type, undergo a widening conversion
(upcast)
<http://java.sun.com/docs/books/jls/third_edition/html/
conversions.html#5.1.5>
or undergo a narrowing conversion (downcast) without a
ClassCastException
<http://java.sun.com/docs/books/jls/third_edition/html/
conversions.html#5.1.6>
..

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
insert track reference in mp3 track D@Z Computer Support 1 05-02-2008 03:32 PM
failing to instantiate an inner class because of order of inner classes Pyenos Python 2 12-27-2006 11:19 PM
Old certification track vs new certification track. Barkley Hughes MCAD 5 11-12-2006 02:41 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments