aspose file tools*
The moose likes Beginning Java and the fly likes Use of a static method with queues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use of a static method with queues" Watch "Use of a static method with queues" New topic
Author

Use of a static method with queues

Julian King
Greenhorn

Joined: Mar 11, 2002
Posts: 1
Hi, I'm a Java newbie and I've been really stuck on this assignment in the Osborne Java 2 book. I really would appreciate any help, even a clue or two?
In this application there are classes specifying types of queue formed in arrays, these in themselves aren't a problem to me, but where I fall down a hole is the bit where I've been asked to write a static method to copy the contents from one queue to another.
So far I've got:
class Copy {
public static int[] a;
public static int[] b;
public static int[] StaticCopy() {
a = b;
return a;
}
}
This seems to compile but I get nothing but a null value from it, because obviously it's not referencing the instance variable arrays in the queue classes. Being static the method can only reference static variables, and yet the queue arrays are non-static. Also, in the main method I shouldn't have to create an object of class Copy should I because it's static. How can I reference the non-static queue arrays, which are each in their separate classes?
As you can see I'm a bit blurry on the whole concept and any nudge in the right direction would be much appreciated.
Thanks

Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Can you post your complete code? You say your arrays are not static, and yet their declaration shows they are indeed static:
public static int[] a;
public static int[] b;
Another thing, your method isn't copying the array contents, it's merely assigning to reference variable a the same value as reference variable b. What this means is that after the assignment, both a and b are referring to the same array in memory.
For example, if the int[] has these values: {1,2,3} then the expressions a[1] and b[1] both refer to element 1 of the same array and have value = 2.
If you want to actually make a seperate copy of the arry you have to either implement your own method that:
1. creates a new int[] of the same size
2. loops over this new array, copying each element from the first array into the copy array.
You can also use the lava.lang.System method System.arrayCopy(). Check out the java docs on this method.


Rob
SCJP 1.4
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,



[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
And just to point out a few of the potential pitfalls of class Copy:
- the array lengths aren't really considered to be of concern (they very well may be - especially if they are of different lengths)
- maybe you'd rather have just one array as the parameter and use the static method to copy the elements into a new array that is returned
- if you are using arrays of Objects (not primitives) then the copying method would have to not just copy references to the elements from one array into the other, but (it'd probably be preferred to) copy new copied instances of the Objects from the input array
You might want to take a look at System.arraycopy for design considerations (but I don't recommend trying to find the source code in the JDK and JVM source files for how the platform specific details of it work - I tried and it ain't worth it).
Good Luck.
[ March 11, 2002: Message edited by: Dirk Schreckmann ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Use of a static method with queues